home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 6 / CU Amiga Magazine's Super CD-ROM 06 (1996)(EMAP Images)(GB)(Track 1 of 4)[!][issue 1997-01].iso / cucd / prog / gnu-c / src / gcc-2.7.0-amiga / cp / call.c next >
C/C++ Source or Header  |  1995-06-15  |  87KB  |  2,981 lines

  1. /* Functions related to invoking methods and overloaded functions.
  2.    Copyright (C) 1987, 1992, 1993 Free Software Foundation, Inc.
  3.    Contributed by Michael Tiemann (tiemann@cygnus.com) and
  4.    hacked by Brendan Kehoe (brendan@cygnus.com).
  5.  
  6. This file is part of GNU CC.
  7.  
  8. GNU CC is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2, or (at your option)
  11. any later version.
  12.  
  13. GNU CC is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with GNU CC; see the file COPYING.  If not, write to
  20. the Free Software Foundation, 59 Temple Place - Suite 330,
  21. Boston, MA 02111-1307, USA.  */
  22.  
  23.  
  24. /* High-level class interface. */
  25.  
  26. #include "config.h"
  27. #include "tree.h"
  28. #include <stdio.h>
  29. #include "cp-tree.h"
  30. #include "class.h"
  31. #include "output.h"
  32. #include "flags.h"
  33.  
  34. #include "obstack.h"
  35. #define obstack_chunk_alloc xmalloc
  36. #define obstack_chunk_free free
  37.  
  38. extern void sorry ();
  39.  
  40. extern int inhibit_warnings;
  41. extern int flag_assume_nonnull_objects;
  42. extern tree ctor_label, dtor_label;
  43.  
  44. /* From typeck.c:  */
  45. extern tree unary_complex_lvalue ();
  46.  
  47. /* Compute the ease with which a conversion can be performed
  48.    between an expected and the given type.  */
  49. static struct harshness_code convert_harshness ();
  50.  
  51. #define EVIL_RETURN(ARG)    ((ARG).code = EVIL_CODE, (ARG))
  52. #define STD_RETURN(ARG)        ((ARG).code = STD_CODE, (ARG))
  53. #define QUAL_RETURN(ARG)    ((ARG).code = QUAL_CODE, (ARG))
  54. #define TRIVIAL_RETURN(ARG)    ((ARG).code = TRIVIAL_CODE, (ARG))
  55. #define ZERO_RETURN(ARG)    ((ARG).code = 0, (ARG))
  56.  
  57. /* Ordering function for overload resolution.  Compare two candidates
  58.    by gross quality.  */
  59. int
  60. rank_for_overload (x, y)
  61.      struct candidate *x, *y;
  62. {
  63.   if (y->h.code & (EVIL_CODE|ELLIPSIS_CODE|USER_CODE))
  64.     return y->h.code - x->h.code;
  65.   if (x->h.code & (EVIL_CODE|ELLIPSIS_CODE|USER_CODE))
  66.     return -1;
  67.  
  68.   /* This is set by compute_conversion_costs, for calling a non-const
  69.      member function from a const member function.  */
  70.   if ((y->harshness[0].code & CONST_CODE) ^ (x->harshness[0].code & CONST_CODE))
  71.     return y->harshness[0].code - x->harshness[0].code;
  72.  
  73.   if (y->h.code & STD_CODE)
  74.     {
  75.       if (x->h.code & STD_CODE)
  76.     return y->h.distance - x->h.distance;
  77.       return 1;
  78.     }
  79.   if (x->h.code & STD_CODE)
  80.     return -1;
  81.  
  82.   return y->h.code - x->h.code;
  83. }
  84.  
  85. /* Compare two candidates, argument by argument.  */
  86. int
  87. rank_for_ideal (x, y)
  88.      struct candidate *x, *y;
  89. {
  90.   int i;
  91.  
  92.   if (x->h_len != y->h_len)
  93.     abort ();
  94.  
  95.   for (i = 0; i < x->h_len; i++)
  96.     {
  97.       if (y->harshness[i].code - x->harshness[i].code)
  98.     return y->harshness[i].code - x->harshness[i].code;
  99.       if ((y->harshness[i].code & STD_CODE)
  100.       && (y->harshness[i].distance - x->harshness[i].distance))
  101.     return y->harshness[i].distance - x->harshness[i].distance;
  102.  
  103.       /* They're both the same code.  Now see if we're dealing with an
  104.      integral promotion that needs a finer grain of accuracy.  */
  105.       if (y->harshness[0].code & PROMO_CODE
  106.       && (y->harshness[i].int_penalty ^ x->harshness[i].int_penalty))
  107.     return y->harshness[i].int_penalty - x->harshness[i].int_penalty;
  108.     }
  109.   return 0;
  110. }
  111.  
  112. /* TYPE is the type we wish to convert to.  PARM is the parameter
  113.    we have to work with.  We use a somewhat arbitrary cost function
  114.    to measure this conversion.  */
  115. static struct harshness_code
  116. convert_harshness (type, parmtype, parm)
  117.      register tree type, parmtype;
  118.      tree parm;
  119. {
  120.   struct harshness_code h;
  121.   register enum tree_code codel;
  122.   register enum tree_code coder;
  123.   int lvalue;
  124.  
  125.   h.code = 0;
  126.   h.distance = 0;
  127.   h.int_penalty = 0;
  128.  
  129. #ifdef GATHER_STATISTICS
  130.   n_convert_harshness++;
  131. #endif
  132.  
  133.   if (TREE_CODE (parmtype) == REFERENCE_TYPE)
  134.     {
  135.       if (parm)
  136.     parm = convert_from_reference (parm);
  137.       parmtype = TREE_TYPE (parmtype);
  138.       lvalue = 1;
  139.     }
  140.   else if (parm)
  141.     lvalue = lvalue_p (parm);
  142.   else
  143.     lvalue = 0;
  144.  
  145.   if (TYPE_PTRMEMFUNC_P (type))
  146.     type = TYPE_PTRMEMFUNC_FN_TYPE (type);
  147.   if (TYPE_PTRMEMFUNC_P (parmtype))
  148.     parmtype = TYPE_PTRMEMFUNC_FN_TYPE (parmtype);
  149.  
  150.   codel = TREE_CODE (type);
  151.   coder = TREE_CODE (parmtype);
  152.  
  153.   if (TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (type))
  154.     return ZERO_RETURN (h);
  155.  
  156.   if (coder == ERROR_MARK)
  157.     return EVIL_RETURN (h);
  158.  
  159.   if (codel == POINTER_TYPE && fntype_p (parmtype))
  160.     {
  161.       tree p1, p2;
  162.       struct harshness_code h1, h2;
  163.  
  164.       /* Get to the METHOD_TYPE or FUNCTION_TYPE that this might be.  */
  165.       type = TREE_TYPE (type);
  166.  
  167.       if (coder == POINTER_TYPE)
  168.     {
  169.       parmtype = TREE_TYPE (parmtype);
  170.       coder = TREE_CODE (parmtype);
  171.     }
  172.  
  173.       if (coder != TREE_CODE (type))
  174.     return EVIL_RETURN (h);
  175.  
  176.       if (type != parmtype && coder == METHOD_TYPE)
  177.     {
  178.       tree ttl = TYPE_METHOD_BASETYPE (type);
  179.       tree ttr = TYPE_METHOD_BASETYPE (parmtype);
  180.  
  181.       int b_or_d = get_base_distance (ttr, ttl, 0, 0);
  182.       if (b_or_d < 0)
  183.         {
  184.           b_or_d = get_base_distance (ttl, ttr, 0, 0);
  185.           if (b_or_d < 0)
  186.         return EVIL_RETURN (h);
  187.           h.distance = -b_or_d;
  188.         }
  189.       else
  190.         h.distance = b_or_d;
  191.       h.code = STD_CODE;
  192.  
  193.       type = build_function_type
  194.         (TREE_TYPE (type), TREE_CHAIN (TYPE_ARG_TYPES (type)));
  195.       parmtype = build_function_type
  196.         (TREE_TYPE (parmtype), TREE_CHAIN (TYPE_ARG_TYPES (parmtype)));
  197.     }
  198.  
  199.       /* We allow the default conversion between function type
  200.      and pointer-to-function type for free.  */
  201.       if (type == parmtype)
  202.     return h;
  203.  
  204.       if (pedantic)
  205.     return EVIL_RETURN (h);
  206.  
  207.       /* Compare return types.  */
  208.       p1 = TREE_TYPE (type);
  209.       p2 = TREE_TYPE (parmtype);
  210.       h2 = convert_harshness (p1, p2, NULL_TREE);
  211.       if (h2.code & EVIL_CODE)
  212.     return h2;
  213.  
  214.       h1.code = TRIVIAL_CODE;
  215.       h1.distance = 0;
  216.  
  217.       if (h2.distance != 0)
  218.     {
  219.       tree binfo;
  220.  
  221.       /* This only works for pointers.  */
  222.       if (TREE_CODE (p1) != POINTER_TYPE
  223.           && TREE_CODE (p1) != REFERENCE_TYPE)
  224.         return EVIL_RETURN (h);
  225.  
  226.       p1 = TREE_TYPE (p1);
  227.       p2 = TREE_TYPE (p2);
  228.       /* Don't die if we happen to be dealing with void*.  */
  229.       if (!IS_AGGR_TYPE (p1) || !IS_AGGR_TYPE (p2))
  230.         return EVIL_RETURN (h);
  231.       if (h2.distance < 0)
  232.         binfo = get_binfo (p2, p1, 0);
  233.       else
  234.         binfo = get_binfo (p1, p2, 0);
  235.  
  236.       if (! BINFO_OFFSET_ZEROP (binfo))
  237.         {
  238. #if 0
  239.           static int explained = 0;
  240.           if (h2.distance < 0)
  241.         message_2_types (sorry, "cannot cast `%s' to `%s' at function call site", p2, p1);
  242.           else
  243.         message_2_types (sorry, "cannot cast `%s' to `%s' at function call site", p1, p2);
  244.  
  245.           if (! explained++)
  246.         sorry ("(because pointer values change during conversion)");
  247. #endif
  248.           return EVIL_RETURN (h);
  249.         }
  250.     }
  251.  
  252.       h1.code |= h2.code;
  253.       if (h2.distance > h1.distance)
  254.     h1.distance = h2.distance;
  255.  
  256.       p1 = TYPE_ARG_TYPES (type);
  257.       p2 = TYPE_ARG_TYPES (parmtype);
  258.       while (p1 && TREE_VALUE (p1) != void_type_node
  259.          && p2 && TREE_VALUE (p2) != void_type_node)
  260.     {
  261.       h2 = convert_harshness (TREE_VALUE (p1), TREE_VALUE (p2),
  262.                        NULL_TREE);
  263.       if (h2.code & EVIL_CODE)
  264.         return h2;
  265.  
  266.       if (h2.distance)
  267.         {
  268.           /* This only works for pointers and references. */
  269.           if (TREE_CODE (TREE_VALUE (p1)) != POINTER_TYPE
  270.           && TREE_CODE (TREE_VALUE (p1)) != REFERENCE_TYPE)
  271.         return EVIL_RETURN (h);
  272.           h2.distance = - h2.distance;
  273.         }
  274.  
  275.       h1.code |= h2.code;
  276.       if (h2.distance > h1.distance)
  277.         h1.distance = h2.distance;
  278.       p1 = TREE_CHAIN (p1);
  279.       p2 = TREE_CHAIN (p2);
  280.     }
  281.       if (p1 == p2)
  282.     return h1;
  283.       if (p2)
  284.     {
  285.       if (p1)
  286.         return EVIL_RETURN (h);
  287.       h1.code |= ELLIPSIS_CODE;
  288.       return h1;
  289.     }
  290.       if (p1)
  291.     {
  292.       if (TREE_PURPOSE (p1) == NULL_TREE)
  293.         h1.code |= EVIL_CODE;
  294.       return h1;
  295.     }
  296.     }
  297.   else if (codel == POINTER_TYPE && coder == OFFSET_TYPE)
  298.     {
  299.       tree ttl, ttr;
  300.  
  301.       /* Get to the OFFSET_TYPE that this might be.  */
  302.       type = TREE_TYPE (type);
  303.  
  304.       if (coder != TREE_CODE (type))
  305.     return EVIL_RETURN (h);
  306.  
  307.       ttl = TYPE_OFFSET_BASETYPE (type);
  308.       ttr = TYPE_OFFSET_BASETYPE (parmtype);
  309.  
  310.       if (ttl == ttr)
  311.     h.code = 0;
  312.       else
  313.     {
  314.       int b_or_d = get_base_distance (ttr, ttl, 0, 0);
  315.       if (b_or_d < 0)
  316.         {
  317.           b_or_d = get_base_distance (ttl, ttr, 0, 0);
  318.           if (b_or_d < 0)
  319.         return EVIL_RETURN (h);
  320.           h.distance = -b_or_d;
  321.         }
  322.       else
  323.         h.distance = b_or_d;
  324.       h.code = STD_CODE;
  325.     }
  326.  
  327.       /* Now test the OFFSET_TYPE's target compatibility.  */
  328.       type = TREE_TYPE (type);
  329.       parmtype = TREE_TYPE (parmtype);
  330.     }
  331.  
  332.   if (coder == UNKNOWN_TYPE)
  333.     {
  334.       if (codel == FUNCTION_TYPE
  335.       || codel == METHOD_TYPE
  336.       || (codel == POINTER_TYPE
  337.           && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
  338.           || TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)))
  339.     return TRIVIAL_RETURN (h);
  340.       return EVIL_RETURN (h);
  341.     }
  342.  
  343.   if (coder == VOID_TYPE)
  344.     return EVIL_RETURN (h);
  345.  
  346.   if (codel == BOOLEAN_TYPE)
  347.     {
  348.       if (INTEGRAL_CODE_P (coder) || coder == REAL_TYPE)
  349.     return STD_RETURN (h);
  350.       else if (coder == POINTER_TYPE || coder == OFFSET_TYPE)
  351.     {
  352.       /* Make this worse than any conversion to another pointer.
  353.          FIXME this is how I think the language should work, but it may not
  354.          end up being how the language is standardized (jason 1/30/95).  */
  355.       h.distance = 32767;
  356.       return STD_RETURN (h);
  357.     }
  358.       return EVIL_RETURN (h);
  359.     }
  360.  
  361.   if (INTEGRAL_CODE_P (codel))
  362.     {
  363.       /* Control equivalence of ints an enums.  */
  364.  
  365.       if (codel == ENUMERAL_TYPE
  366.       && flag_int_enum_equivalence == 0)
  367.     {
  368.       /* Enums can be converted to ints, but not vice-versa.  */
  369.       if (coder != ENUMERAL_TYPE
  370.           || TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (parmtype))
  371.         return EVIL_RETURN (h);
  372.     }
  373.  
  374.       /* else enums and ints (almost) freely interconvert.  */
  375.  
  376.       if (INTEGRAL_CODE_P (coder))
  377.     {
  378.       if (TYPE_MAIN_VARIANT (type)
  379.           == TYPE_MAIN_VARIANT (type_promotes_to (parmtype)))
  380.         {
  381.           h.code = PROMO_CODE;
  382. #if 0 /* What purpose does this serve?  -jason */
  383.           /* A char, short, wchar_t, etc., should promote to an int if
  384.          it can handle it, otherwise to an unsigned.  So we'll make
  385.          an unsigned.  */
  386.           if (type != integer_type_node)
  387.         h.int_penalty = 1;
  388. #endif
  389.         }
  390.       else
  391.         h.code = STD_CODE;
  392.         
  393.       return h;
  394.     }
  395.       else if (coder == REAL_TYPE)
  396.     {
  397.       h.code = STD_CODE;
  398.       h.distance = 0;
  399.       return h;
  400.     }
  401.     }
  402.  
  403.   if (codel == REAL_TYPE)
  404.     {
  405.       if (coder == REAL_TYPE)
  406.     {
  407.       if (TYPE_MAIN_VARIANT (type)
  408.           == TYPE_MAIN_VARIANT (type_promotes_to (parmtype)))
  409.         h.code = PROMO_CODE;
  410.       else
  411.         h.code = STD_CODE;
  412.         
  413.       return h;
  414.     }
  415.       else if (INTEGRAL_CODE_P (coder))
  416.     {
  417.       h.code = STD_CODE;
  418.       h.distance = 0;
  419.       return h;
  420.     }
  421.     }
  422.  
  423.   /* Convert arrays which have not previously been converted.  */
  424.   if (codel == ARRAY_TYPE)
  425.     codel = POINTER_TYPE;
  426.   if (coder == ARRAY_TYPE)
  427.     coder = POINTER_TYPE;
  428.  
  429.   /* Conversions among pointers */
  430.   if (codel == POINTER_TYPE && coder == POINTER_TYPE)
  431.     {
  432.       register tree ttl = TYPE_MAIN_VARIANT (TREE_TYPE (type));
  433.       register tree ttr = TYPE_MAIN_VARIANT (TREE_TYPE (parmtype));
  434.       int penalty = 4 * (ttl != ttr);
  435.  
  436.       /* Anything converts to void *.  Since this may be `const void *'
  437.      (etc.) use VOID_TYPE instead of void_type_node.  Otherwise, the
  438.      targets must be the same, except that we do allow (at some cost)
  439.      conversion between signed and unsigned pointer types.  */
  440.  
  441.       if ((TREE_CODE (ttl) == METHOD_TYPE
  442.        || TREE_CODE (ttl) == FUNCTION_TYPE)
  443.       && TREE_CODE (ttl) == TREE_CODE (ttr))
  444.     {
  445.       if (comptypes (ttl, ttr, -1))
  446.         {
  447.           h.code = penalty ? STD_CODE : 0;
  448.           h.distance =  0;
  449.         }
  450.       else
  451.         h.code = EVIL_CODE;
  452.       return h;
  453.     }
  454.  
  455. #if 1
  456.       if (TREE_CODE (ttl) != VOID_TYPE
  457.       && (TREE_CODE (ttr) != VOID_TYPE || !parm || !integer_zerop (parm)))
  458.     {
  459.       if (TREE_UNSIGNED (ttl) != TREE_UNSIGNED (ttr))
  460.         {
  461.           ttl = unsigned_type (ttl);
  462.           ttr = unsigned_type (ttr);
  463.           penalty = 10;
  464.         }
  465.       if (comp_target_types (ttl, ttr, 0) <= 0)
  466.         return EVIL_RETURN (h);
  467.     }
  468. #else
  469.       if (!(TREE_CODE (ttl) == VOID_TYPE
  470.         || TREE_CODE (ttr) == VOID_TYPE
  471.         || (TREE_UNSIGNED (ttl) ^ TREE_UNSIGNED (ttr)
  472.         && (ttl = unsigned_type (ttl),
  473.             ttr = unsigned_type (ttr),
  474.             penalty = 10, 0))
  475.         || (comp_target_types (ttl, ttr, 0) > 0)))
  476.     return EVIL_RETURN (h);
  477. #endif
  478.  
  479.       if (penalty == 10 || ttr == ttl)
  480.     {
  481.       tree tmp1 = TREE_TYPE (type), tmp2 = TREE_TYPE (parmtype);
  482.  
  483.       /* If one was unsigned but the other wasn't, then we need to
  484.          do a standard conversion from T to unsigned T.  */
  485.       if (penalty == 10)
  486.         h.code = PROMO_CODE; /* was STD_CODE */
  487.       else
  488.         h.code = 0;
  489.  
  490.       /* Note conversion from `T*' to `const T*',
  491.                            or `T*' to `volatile T*'.  */
  492.       if (ttl == ttr
  493.           && ((TYPE_READONLY (tmp1) != TREE_READONLY (tmp2))
  494.           || (TYPE_VOLATILE (tmp1) != TYPE_VOLATILE (tmp2))))
  495.         h.code |= QUAL_CODE;
  496.  
  497.       h.distance = 0;
  498.       return h;
  499.     }
  500.  
  501.  
  502.       if (TREE_CODE (ttl) == RECORD_TYPE && TREE_CODE (ttr) == RECORD_TYPE)
  503.     {
  504.       int b_or_d = get_base_distance (ttl, ttr, 0, 0);
  505.       if (b_or_d < 0)
  506.         {
  507.           b_or_d = get_base_distance (ttr, ttl, 0, 0);
  508.           if (b_or_d < 0)
  509.         return EVIL_RETURN (h);
  510.           h.distance = -b_or_d;
  511.         }
  512.       else
  513.         h.distance = b_or_d;
  514.       h.code = STD_CODE;
  515.       return h;
  516.     }
  517.  
  518.       /* If converting from a `class*' to a `void*', make it
  519.      less favorable than any inheritance relationship.  */
  520.       if (TREE_CODE (ttl) == VOID_TYPE && IS_AGGR_TYPE (ttr))
  521.     {
  522.       h.code = STD_CODE;
  523.       h.distance = CLASSTYPE_MAX_DEPTH (ttr)+1;
  524.       return h;
  525.     }
  526.       h.code = penalty ? STD_CODE : PROMO_CODE;
  527.       return h;
  528.     }
  529.  
  530.   if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
  531.     {
  532.       /* This is not a bad match, but don't let it beat
  533.      integer-enum combinations.  */
  534.       if (parm && integer_zerop (parm))
  535.     {
  536.       h.code = STD_CODE;
  537.       h.distance = 0;
  538.       return h;
  539.     }
  540.     }
  541.  
  542.   /* C++: Since the `this' parameter of a signature member function
  543.      is represented as a signature pointer to handle default implementations
  544.      correctly, we can have the case that `type' is a signature pointer
  545.      while `parmtype' is a pointer to a signature table.  We don't really
  546.      do any conversions in this case, so just return 0.  */
  547.  
  548.   if (codel == RECORD_TYPE && coder == POINTER_TYPE
  549.       && IS_SIGNATURE_POINTER (type) && IS_SIGNATURE (TREE_TYPE (parmtype)))
  550.     return ZERO_RETURN (h);
  551.  
  552.   if (codel == REFERENCE_TYPE)
  553.     {
  554.       tree ttl, ttr;
  555.       int constp = parm ? TREE_READONLY (parm) : TYPE_READONLY (parmtype);
  556.       int volatilep = (parm ? TREE_THIS_VOLATILE (parm)
  557.                : TYPE_VOLATILE (parmtype));
  558.       register tree intype = TYPE_MAIN_VARIANT (parmtype);
  559.       register enum tree_code form = TREE_CODE (intype);
  560.       int penalty = 0;
  561.  
  562.       ttl = TREE_TYPE (type);
  563.  
  564.       /* Only allow const reference binding if we were given a parm to deal
  565.          with, since it isn't really a conversion.  This is a hack to
  566.          prevent build_type_conversion from finding this conversion, but
  567.          still allow overloading to find it.  */
  568.       if (! lvalue && ! (parm && TYPE_READONLY (ttl)))
  569.     return EVIL_RETURN (h);
  570.  
  571.       if (TYPE_READONLY (ttl) < constp
  572.       || TYPE_VOLATILE (ttl) < volatilep)
  573.     return EVIL_RETURN (h);
  574.  
  575.       /* When passing a non-const argument into a const reference, dig it a
  576.      little, so a non-const reference is preferred over this one.  */
  577.       penalty = ((TYPE_READONLY (ttl) > constp)
  578.          + (TYPE_VOLATILE (ttl) > volatilep));
  579.  
  580.       ttl = TYPE_MAIN_VARIANT (ttl);
  581.  
  582.       if (form == OFFSET_TYPE)
  583.     {
  584.       intype = TREE_TYPE (intype);
  585.       form = TREE_CODE (intype);
  586.     }
  587.  
  588.       ttr = intype;
  589.  
  590.       /* Maybe handle conversion to base here?  */
  591.  
  592.       h = convert_harshness (ttl, ttr, NULL_TREE);
  593.       if (penalty && h.code == 0)
  594.     {
  595.       h.code = QUAL_CODE;
  596.       h.int_penalty = penalty;
  597.     }
  598.       return h;
  599.     }
  600.   if (codel == RECORD_TYPE && coder == RECORD_TYPE)
  601.     {
  602.       int b_or_d = get_base_distance (type, parmtype, 0, 0);
  603.       if (b_or_d < 0)
  604.     {
  605.       b_or_d = get_base_distance (parmtype, type, 0, 0);
  606.       if (b_or_d < 0)
  607.         return EVIL_RETURN (h);
  608.       h.distance = -b_or_d;
  609.     }
  610.       else
  611.     h.distance = b_or_d;
  612.       h.code = STD_CODE;
  613.       return h;
  614.     }
  615.   return EVIL_RETURN (h);
  616. }
  617.  
  618. int
  619. user_harshness (type, parmtype, parm)
  620.      register tree type, parmtype;
  621.      tree parm;
  622. {
  623.   tree conv;
  624.   tree winner = NULL_TREE;
  625.   int code;
  626.  
  627.   {
  628.     tree typename = build_typename_overload (type);
  629.     if (lookup_fnfields (TYPE_BINFO (parmtype), typename, 0))
  630.       return 0;
  631.   }
  632.             
  633.   for (conv = lookup_conversions (parmtype); conv; conv = TREE_CHAIN (conv))
  634.     {
  635.       struct harshness_code tmp;
  636.  
  637.       if (winner && TREE_PURPOSE (winner) == TREE_PURPOSE (conv))
  638.     continue;
  639.  
  640.       if (tmp = convert_harshness (type, TREE_VALUE (conv), NULL_TREE),
  641.       tmp.code < USER_CODE)
  642.     {
  643.       if (winner)
  644.         return EVIL_CODE;
  645.       else
  646.         {
  647.           winner = conv;
  648.           code = tmp.code;
  649.         }
  650.     }
  651.     }
  652.  
  653.   if (winner)
  654.     return code;
  655.  
  656.   return -1;
  657. }
  658.  
  659. int
  660. can_convert (to, from)
  661.      tree to, from;
  662. {
  663.   struct harshness_code h;
  664.   h = convert_harshness (to, from, NULL_TREE);
  665.   return h.code < USER_CODE;
  666. }
  667.  
  668. int
  669. can_convert_arg (to, from, arg)
  670.      tree to, from, arg;
  671. {
  672.   struct harshness_code h;
  673.   h = convert_harshness (to, from, arg);
  674.   return h.code < USER_CODE;
  675. }
  676.  
  677. #ifdef DEBUG_MATCHING
  678. static char *
  679. print_harshness (h)
  680.      struct harshness_code *h;
  681. {
  682.   static char buf[1024];
  683.   char tmp[1024];
  684.  
  685.   bzero (buf, 1024 * sizeof (char));
  686.   strcat (buf, "codes=[");
  687.   if (h->code & EVIL_CODE)
  688.     strcat (buf, "EVIL");
  689.   if (h->code & CONST_CODE)
  690.     strcat (buf, " CONST");
  691.   if (h->code & ELLIPSIS_CODE)
  692.     strcat (buf, " ELLIPSIS");
  693.   if (h->code & USER_CODE)
  694.     strcat (buf, " USER");
  695.   if (h->code & STD_CODE)
  696.     strcat (buf, " STD");
  697.   if (h->code & PROMO_CODE)
  698.     strcat (buf, " PROMO");
  699.   if (h->code & QUAL_CODE)
  700.     strcat (buf, " QUAL");
  701.   if (h->code & TRIVIAL_CODE)
  702.     strcat (buf, " TRIVIAL");
  703.   if (buf[0] == '\0')
  704.     strcat (buf, "0");
  705.  
  706.   sprintf (tmp, "] distance=%d int_penalty=%d", h->distance, h->int_penalty);
  707.  
  708.   strcat (buf, tmp);
  709.  
  710.   return buf;
  711. }
  712. #endif
  713.  
  714. /* Algorithm: For each argument, calculate how difficult it is to
  715.    make FUNCTION accept that argument.  If we can easily tell that
  716.    FUNCTION won't be acceptable to one of the arguments, then we
  717.    don't need to compute the ease of converting the other arguments,
  718.    since it will never show up in the intersection of all arguments'
  719.    favorite functions.
  720.  
  721.    Conversions between builtin and user-defined types are allowed, but
  722.    no function involving such a conversion is preferred to one which
  723.    does not require such a conversion.  Furthermore, such conversions
  724.    must be unique.  */
  725.  
  726. void
  727. compute_conversion_costs (function, tta_in, cp, arglen)
  728.      tree function;
  729.      tree tta_in;
  730.      struct candidate *cp;
  731.      int arglen;
  732. {
  733.   tree ttf_in = TYPE_ARG_TYPES (TREE_TYPE (function));
  734.   tree ttf = ttf_in;
  735.   tree tta = tta_in;
  736.  
  737.   /* Start out with no strikes against.  */
  738.   int evil_strikes = 0;
  739.   int ellipsis_strikes = 0;
  740.   int user_strikes = 0;
  741.   int b_or_d_strikes = 0;
  742.   int easy_strikes = 0;
  743.  
  744.   int strike_index = 0, win;
  745.   struct harshness_code lose;
  746.   extern int cp_silent;
  747.  
  748. #ifdef GATHER_STATISTICS
  749.   n_compute_conversion_costs++;
  750. #endif
  751.  
  752. #ifndef DEBUG_MATCHING
  753.   /* We don't emit any warnings or errors while trying out each candidate.  */
  754.   cp_silent = 1;
  755. #endif
  756.  
  757.   cp->function = function;
  758.   cp->arg = tta ? TREE_VALUE (tta) : NULL_TREE;
  759.   cp->u.bad_arg = 0;        /* optimistic!  */
  760.  
  761.   cp->h.code = 0;
  762.   cp->h.distance = 0;
  763.   cp->h.int_penalty = 0;
  764.   bzero ((char *) cp->harshness,
  765.      (cp->h_len + 1) * sizeof (struct harshness_code));
  766.  
  767.   while (ttf && tta)
  768.     {
  769.       struct harshness_code h;
  770.  
  771.       if (ttf == void_list_node)
  772.     break;
  773.  
  774.       if (type_unknown_p (TREE_VALUE (tta)))
  775.     {      
  776.       /* Must perform some instantiation here.  */
  777.       tree rhs = TREE_VALUE (tta);
  778.       tree lhstype = TREE_VALUE (ttf);
  779.  
  780.       /* Keep quiet about possible contravariance violations.  */
  781.       int old_inhibit_warnings = inhibit_warnings;
  782.       inhibit_warnings = 1;
  783.  
  784.       /* @@ This is to undo what `grokdeclarator' does to
  785.          parameter types.  It really should go through
  786.          something more general.  */
  787.  
  788.       TREE_TYPE (tta) = unknown_type_node;
  789.       rhs = instantiate_type (lhstype, rhs, 0);
  790.       inhibit_warnings = old_inhibit_warnings;
  791.  
  792.       if (TREE_CODE (rhs) == ERROR_MARK)
  793.         h.code = EVIL_CODE;
  794.       else
  795.         h = convert_harshness (lhstype, TREE_TYPE (rhs), rhs);
  796.     }
  797.       else
  798.     {
  799. #ifdef DEBUG_MATCHING
  800.       static tree old_function = NULL_TREE;
  801.  
  802.       if (!old_function || function != old_function)
  803.         {
  804.           cp_error ("trying %D", function);
  805.           old_function = function;
  806.         }
  807.  
  808.       cp_error ("      doing (%T) %E against arg %T",
  809.             TREE_TYPE (TREE_VALUE (tta)), TREE_VALUE (tta),
  810.             TREE_VALUE (ttf));
  811. #endif
  812.  
  813.       h = convert_harshness (TREE_VALUE (ttf),
  814.                  TREE_TYPE (TREE_VALUE (tta)),
  815.                  TREE_VALUE (tta));
  816.  
  817. #ifdef DEBUG_MATCHING
  818.       cp_error ("     evaluated %s", print_harshness (&h));
  819. #endif
  820.     }
  821.  
  822.       cp->harshness[strike_index] = h;
  823.       if ((h.code & EVIL_CODE)
  824.       || ((h.code & STD_CODE) && h.distance < 0))
  825.     {
  826.       cp->u.bad_arg = strike_index;
  827.       evil_strikes = 1;
  828.     }
  829.      else if (h.code & ELLIPSIS_CODE)
  830.        ellipsis_strikes += 1;
  831. #if 0
  832.       /* This is never set by `convert_harshness'.  */
  833.       else if (h.code & USER_CODE)
  834.     {
  835.       user_strikes += 1;
  836.     }
  837. #endif
  838.       else
  839.     {
  840.       if ((h.code & STD_CODE) && h.distance)
  841.         {
  842.           if (h.distance > b_or_d_strikes)
  843.         b_or_d_strikes = h.distance;
  844.         }
  845.       else
  846.         easy_strikes += (h.code & (STD_CODE|PROMO_CODE|TRIVIAL_CODE));
  847.       cp->h.code |= h.code;
  848.       /* Make sure we communicate this.  */
  849.       cp->h.int_penalty += h.int_penalty;
  850.     }
  851.  
  852.       ttf = TREE_CHAIN (ttf);
  853.       tta = TREE_CHAIN (tta);
  854.       strike_index += 1;
  855.     }
  856.  
  857.   if (tta)
  858.     {
  859.       /* ran out of formals, and parmlist is fixed size.  */
  860.       if (ttf /* == void_type_node */)
  861.     {
  862.       cp->h.code = EVIL_CODE;
  863.       cp->u.bad_arg = -1;
  864.       cp_silent = 0;
  865.       return;
  866.     }
  867.       else
  868.     {
  869.       struct harshness_code h;
  870.       int l = list_length (tta);
  871.       ellipsis_strikes += l;
  872.       h.code = ELLIPSIS_CODE;
  873.       h.distance = 0;
  874.       h.int_penalty = 0;
  875.       for (; l; --l)
  876.         cp->harshness[strike_index++] = h;
  877.     }
  878.     }
  879.   else if (ttf && ttf != void_list_node)
  880.     {
  881.       /* ran out of actuals, and no defaults.  */
  882.       if (TREE_PURPOSE (ttf) == NULL_TREE)
  883.     {
  884.       cp->h.code = EVIL_CODE;
  885.       cp->u.bad_arg = -2;
  886.       cp_silent = 0;
  887.       return;
  888.     }
  889.       /* Store index of first default.  */
  890.       cp->harshness[arglen].distance = strike_index+1;
  891.     }
  892.   else
  893.     cp->harshness[arglen].distance = 0;
  894.  
  895.   /* Argument list lengths work out, so don't need to check them again.  */
  896.   if (evil_strikes)
  897.     {
  898.       /* We do not check for derived->base conversions here, since in
  899.      no case would they give evil strike counts, unless such conversions
  900.      are somehow ambiguous.  */
  901.  
  902.       /* See if any user-defined conversions apply.
  903.          But make sure that we do not loop.  */
  904.       static int dont_convert_types = 0;
  905.  
  906.       if (dont_convert_types)
  907.     {
  908.       cp->h.code = EVIL_CODE;
  909.       cp_silent = 0;
  910.       return;
  911.     }
  912.  
  913.       win = 0;            /* Only get one chance to win.  */
  914.       ttf = TYPE_ARG_TYPES (TREE_TYPE (function));
  915.       tta = tta_in;
  916.       strike_index = 0;
  917.       evil_strikes = 0;
  918.  
  919.       while (ttf && tta)
  920.     {
  921.       if (ttf == void_list_node)
  922.         break;
  923.  
  924.       lose = cp->harshness[strike_index];
  925.       if ((lose.code & EVIL_CODE)
  926.           || ((lose.code & STD_CODE) && lose.distance < 0))
  927.         {
  928.           tree actual_type = TREE_TYPE (TREE_VALUE (tta));
  929.           tree formal_type = TREE_VALUE (ttf);
  930.           int extra_conversions = 0;
  931.  
  932.           dont_convert_types = 1;
  933.  
  934.           if (TREE_CODE (formal_type) == REFERENCE_TYPE)
  935.         formal_type = TREE_TYPE (formal_type);
  936.           if (TREE_CODE (actual_type) == REFERENCE_TYPE)
  937.         actual_type = TREE_TYPE (actual_type);
  938.  
  939.           if (formal_type != error_mark_node
  940.           && actual_type != error_mark_node)
  941.         {
  942.           formal_type = TYPE_MAIN_VARIANT (formal_type);
  943.           actual_type = TYPE_MAIN_VARIANT (actual_type);
  944.  
  945.           if (TYPE_HAS_CONSTRUCTOR (formal_type))
  946.             {
  947.               /* If it has a constructor for this type,
  948.              try to use it.  */
  949.               /* @@ There is no way to save this result yet, so
  950.              success is a NULL_TREE for now.  */
  951.               if (convert_to_aggr (formal_type, TREE_VALUE (tta), 0, 1)
  952.               != error_mark_node)
  953.             win++;
  954.             }
  955.           if (TYPE_LANG_SPECIFIC (actual_type)
  956.               && TYPE_HAS_CONVERSION (actual_type))
  957.             {
  958.               int extra = user_harshness (formal_type, actual_type);
  959.  
  960.               if (extra == EVIL_CODE)
  961.             win += 2;
  962.               else if (extra >= 0)
  963.             {
  964.               win++;
  965.               extra_conversions = extra;
  966.             }
  967.             }
  968.         }
  969.           dont_convert_types = 0;
  970.  
  971.           if (win == 1)
  972.         {
  973.           user_strikes += 1;
  974.           cp->harshness[strike_index].code
  975.             = USER_CODE | (extra_conversions ? STD_CODE : 0);
  976.           win = 0;
  977.         }
  978.           else
  979.         {
  980.           if (cp->u.bad_arg > strike_index)
  981.             cp->u.bad_arg = strike_index;
  982.  
  983.           evil_strikes = win ? 2 : 1;
  984.           break;
  985.         }
  986.         }
  987.  
  988.       ttf = TREE_CHAIN (ttf);
  989.       tta = TREE_CHAIN (tta);
  990.       strike_index += 1;
  991.     }
  992.     }
  993.  
  994.   /* Const member functions get a small penalty because defaulting
  995.      to const is less useful than defaulting to non-const. */
  996.   /* This is bogus, it does not correspond to anything in the ARM.
  997.      This code will be fixed when this entire section is rewritten
  998.      to conform to the ARM.  (mrs)  */
  999.   if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
  1000.     {
  1001.       tree this_parm = TREE_VALUE (ttf_in);
  1002.  
  1003.       if (TREE_CODE (this_parm) == RECORD_TYPE    /* Is `this' a sig ptr?  */
  1004.         ? TYPE_READONLY (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (this_parm))))
  1005.         : TYPE_READONLY (TREE_TYPE (this_parm)))
  1006.     {
  1007.       cp->harshness[0].code |= TRIVIAL_CODE;
  1008.       ++easy_strikes;
  1009.     }
  1010.       else
  1011.     {
  1012.       /* Calling a non-const member function from a const member function
  1013.          is probably invalid, but for now we let it only draw a warning.
  1014.          We indicate that such a mismatch has occurred by setting the
  1015.          harshness to a maximum value.  */
  1016.       if (TREE_CODE (TREE_TYPE (TREE_VALUE (tta_in))) == POINTER_TYPE
  1017.           && (TYPE_READONLY (TREE_TYPE (TREE_TYPE (TREE_VALUE (tta_in))))))
  1018.         cp->harshness[0].code |= CONST_CODE;
  1019.     }
  1020.     }
  1021.  
  1022.   if (evil_strikes)
  1023.     cp->h.code = EVIL_CODE;
  1024.   if (ellipsis_strikes)
  1025.     cp->h.code |= ELLIPSIS_CODE;
  1026.   if (user_strikes)
  1027.     cp->h.code |= USER_CODE;
  1028.   cp_silent = 0;
  1029. #ifdef DEBUG_MATCHING
  1030.   cp_error ("final eval %s", print_harshness (&cp->h));
  1031. #endif
  1032. }
  1033.  
  1034. /* Subroutine of ideal_candidate.  See if X or Y is a better match
  1035.    than the other.  */
  1036. static int
  1037. strictly_better (x, y)
  1038.      unsigned short x, y;
  1039. {
  1040.   unsigned short xor;
  1041.  
  1042.   if (x == y)
  1043.     return 0;
  1044.  
  1045.   xor = x ^ y;
  1046.   if (xor >= x || xor >= y)
  1047.     return 1;
  1048.   return 0;
  1049. }
  1050.  
  1051. /* When one of several possible overloaded functions and/or methods
  1052.    can be called, choose the best candidate for overloading.
  1053.  
  1054.    BASETYPE is the context from which we start method resolution
  1055.    or NULL if we are comparing overloaded functions.
  1056.    CANDIDATES is the array of candidates we have to choose from.
  1057.    N_CANDIDATES is the length of CANDIDATES.
  1058.    PARMS is a TREE_LIST of parameters to the function we'll ultimately
  1059.    choose.  It is modified in place when resolving methods.  It is not
  1060.    modified in place when resolving overloaded functions.
  1061.    LEN is the length of the parameter list.  */
  1062.  
  1063. static struct candidate *
  1064. ideal_candidate (basetype, candidates, n_candidates, parms, len)
  1065.      tree basetype;
  1066.      struct candidate *candidates;
  1067.      int n_candidates;
  1068.      tree parms;
  1069.      int len;
  1070. {
  1071.   struct candidate *cp = candidates+n_candidates;
  1072.   int i, j = -1, best_code;
  1073.  
  1074.   /* For each argument, sort the functions from best to worst for the arg.
  1075.      For each function that's not best for this arg, set its overall
  1076.      harshness to EVIL so that other args won't like it.  The candidate
  1077.      list for the last argument is the intersection of all the best-liked
  1078.      functions.  */
  1079.  
  1080. #if 0
  1081.   for (i = 0; i < len; i++)
  1082.     {
  1083.       qsort (candidates, n_candidates, sizeof (struct candidate),
  1084.          rank_for_overload);
  1085.       best_code = cp[-1].h.code;
  1086.  
  1087.       /* To find out functions that are worse than that represented
  1088.      by BEST_CODE, we can't just do a comparison like h.code>best_code.
  1089.      The total harshness for the "best" fn may be 8|8 for two args, and
  1090.      the harshness for the next-best may be 8|2.  If we just compared,
  1091.      that would be checking 8>10, which would lead to the next-best
  1092.      being disqualified.  What we actually want to do is get rid
  1093.      of functions that are definitely worse than that represented
  1094.      by best_code, i.e. those which have bits set higher than the
  1095.      highest in best_code.  Sooooo, what we do is clear out everything
  1096.      represented by best_code, and see if we still come up with something
  1097.      higher.  If so (e.g., 8|8 vs 8|16), it'll disqualify it properly.  */
  1098.       for (j = n_candidates-2; j >= 0; j--)
  1099.     if ((candidates[j].h.code & ~best_code) > best_code)
  1100.       candidates[j].h.code = EVIL_CODE;
  1101.     }
  1102.  
  1103.   if (cp[-1].h.code & EVIL_CODE)
  1104.     return NULL;
  1105. #else
  1106.   qsort (candidates, n_candidates, sizeof (struct candidate),
  1107.      rank_for_overload);
  1108.   best_code = cp[-1].h.code;
  1109. #endif
  1110.  
  1111.   /* If they're at least as good as each other, do an arg-by-arg check.  */
  1112.   if (! strictly_better (cp[-1].h.code, cp[-2].h.code))
  1113.     {
  1114.       int better = 0;
  1115.       int worse = 0;
  1116.  
  1117.       for (j = 0; j < n_candidates; j++)
  1118.     if (! strictly_better (candidates[j].h.code, best_code))
  1119.       break;
  1120.  
  1121.       qsort (candidates+j, n_candidates-j, sizeof (struct candidate),
  1122.          rank_for_ideal);
  1123.       for (i = 0; i < len; i++)
  1124.     {
  1125.       if (cp[-1].harshness[i].code < cp[-2].harshness[i].code)
  1126.         better = 1;
  1127.       else if (cp[-1].harshness[i].code > cp[-2].harshness[i].code)
  1128.         worse = 1;
  1129.       else if (cp[-1].harshness[i].code & STD_CODE)
  1130.         {
  1131.           /* If it involves a standard conversion, let the
  1132.          inheritance lattice be the final arbiter.  */
  1133.           if (cp[-1].harshness[i].distance > cp[-2].harshness[i].distance)
  1134.         worse = 1;
  1135.           else if (cp[-1].harshness[i].distance < cp[-2].harshness[i].distance)
  1136.         better = 1;
  1137.         }
  1138.       else if (cp[-1].harshness[i].code & PROMO_CODE)
  1139.         {
  1140.           /* For integral promotions, take into account a finer
  1141.          granularity for determining which types should be favored
  1142.          over others in such promotions.  */
  1143.           if (cp[-1].harshness[i].int_penalty > cp[-2].harshness[i].int_penalty)
  1144.         worse = 1;
  1145.           else if (cp[-1].harshness[i].int_penalty < cp[-2].harshness[i].int_penalty)
  1146.         better = 1;
  1147.         }
  1148.     }
  1149.  
  1150.       if (! better || worse)
  1151.     return NULL;
  1152.     }
  1153.   return cp-1;
  1154. }
  1155.  
  1156. /* Assume that if the class referred to is not in the
  1157.    current class hierarchy, that it may be remote.
  1158.    PARENT is assumed to be of aggregate type here.  */
  1159. static int
  1160. may_be_remote (parent)
  1161.      tree parent;
  1162. {
  1163.   if (TYPE_OVERLOADS_METHOD_CALL_EXPR (parent) == 0)
  1164.     return 0;
  1165.  
  1166.   if (current_class_type == NULL_TREE)
  1167.     return 0;
  1168.  
  1169.   if (parent == current_class_type)
  1170.     return 0;
  1171.  
  1172.   if (UNIQUELY_DERIVED_FROM_P (parent, current_class_type))
  1173.     return 0;
  1174.   return 1;
  1175. }
  1176.  
  1177. tree
  1178. build_vfield_ref (datum, type)
  1179.      tree datum, type;
  1180. {
  1181.   tree rval;
  1182.   int old_assume_nonnull_objects = flag_assume_nonnull_objects;
  1183.  
  1184.   if (datum == error_mark_node)
  1185.     return error_mark_node;
  1186.  
  1187.   /* Vtable references are always made from non-null objects.  */
  1188.   flag_assume_nonnull_objects = 1;
  1189.   if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
  1190.     datum = convert_from_reference (datum);
  1191.  
  1192.   if (! TYPE_USES_COMPLEX_INHERITANCE (type))
  1193.     rval = build (COMPONENT_REF, TREE_TYPE (CLASSTYPE_VFIELD (type)),
  1194.           datum, CLASSTYPE_VFIELD (type));
  1195.   else
  1196.     rval = build_component_ref (datum, DECL_NAME (CLASSTYPE_VFIELD (type)), 0, 0);
  1197.   flag_assume_nonnull_objects = old_assume_nonnull_objects;
  1198.  
  1199.   return rval;
  1200. }
  1201.  
  1202. /* Build a call to a member of an object.  I.e., one that overloads
  1203.    operator ()(), or is a pointer-to-function or pointer-to-method.  */
  1204. static tree
  1205. build_field_call (basetype_path, instance_ptr, name, parms)
  1206.      tree basetype_path, instance_ptr, name, parms;
  1207. {
  1208.   tree field, instance;
  1209.  
  1210.   if (instance_ptr == current_class_decl)
  1211.     {
  1212.       /* Check to see if we really have a reference to an instance variable
  1213.      with `operator()()' overloaded.  */
  1214.       field = IDENTIFIER_CLASS_VALUE (name);
  1215.  
  1216.       if (field == NULL_TREE)
  1217.     {
  1218.       cp_error ("`this' has no member named `%D'", name);
  1219.       return error_mark_node;
  1220.     }
  1221.  
  1222.       if (TREE_CODE (field) == FIELD_DECL)
  1223.     {
  1224.       /* If it's a field, try overloading operator (),
  1225.          or calling if the field is a pointer-to-function.  */
  1226.       instance = build_component_ref_1 (C_C_D, field, 0);
  1227.       if (instance == error_mark_node)
  1228.         return error_mark_node;
  1229.  
  1230.       if (TYPE_LANG_SPECIFIC (TREE_TYPE (instance))
  1231.           && TYPE_OVERLOADS_CALL_EXPR (TREE_TYPE (instance)))
  1232.         return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, instance, parms, NULL_TREE);
  1233.  
  1234.       if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
  1235.         {
  1236.           if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == FUNCTION_TYPE)
  1237.         return build_function_call (instance, parms);
  1238.           else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == METHOD_TYPE)
  1239.         return build_function_call (instance, tree_cons (NULL_TREE, current_class_decl, parms));
  1240.         }
  1241.     }
  1242.       return NULL_TREE;
  1243.     }
  1244.  
  1245.   /* Check to see if this is not really a reference to an instance variable
  1246.      with `operator()()' overloaded.  */
  1247.   field = lookup_field (basetype_path, name, 1, 0);
  1248.  
  1249.   /* This can happen if the reference was ambiguous or for access
  1250.      violations.  */
  1251.   if (field == error_mark_node)
  1252.     return error_mark_node;
  1253.  
  1254.   if (field)
  1255.     {
  1256.       tree basetype;
  1257.       tree ftype = TREE_TYPE (field);
  1258.  
  1259.       if (TREE_CODE (ftype) == REFERENCE_TYPE)
  1260.     ftype = TREE_TYPE (ftype);
  1261.  
  1262.       if (TYPE_LANG_SPECIFIC (ftype) && TYPE_OVERLOADS_CALL_EXPR (ftype))
  1263.     {
  1264.       /* Make the next search for this field very short.  */
  1265.       basetype = DECL_FIELD_CONTEXT (field);
  1266.       instance_ptr = convert_pointer_to (basetype, instance_ptr);
  1267.  
  1268.       instance = build_indirect_ref (instance_ptr, NULL_PTR);
  1269.       return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
  1270.                  build_component_ref_1 (instance, field, 0),
  1271.                  parms, NULL_TREE);
  1272.     }
  1273.       if (TREE_CODE (ftype) == POINTER_TYPE)
  1274.     {
  1275.       if (TREE_CODE (TREE_TYPE (ftype)) == FUNCTION_TYPE
  1276.           || TREE_CODE (TREE_TYPE (ftype)) == METHOD_TYPE)
  1277.         {
  1278.           /* This is a member which is a pointer to function.  */
  1279.           tree ref
  1280.         = build_component_ref_1 (build_indirect_ref (instance_ptr,
  1281.                                  NULL_PTR),
  1282.                      field, LOOKUP_COMPLAIN);
  1283.           if (ref == error_mark_node)
  1284.         return error_mark_node;
  1285.           return build_function_call (ref, parms);
  1286.         }
  1287.     }
  1288.       else if (TREE_CODE (ftype) == METHOD_TYPE)
  1289.     {
  1290.       error ("invalid call via pointer-to-member function");
  1291.       return error_mark_node;
  1292.     }
  1293.       else
  1294.     return NULL_TREE;
  1295.     }
  1296.   return NULL_TREE;
  1297. }
  1298.  
  1299. tree
  1300. find_scoped_type (type, inner_name, inner_types)
  1301.      tree type, inner_name, inner_types;
  1302. {
  1303.   tree tags = CLASSTYPE_TAGS (type);
  1304.  
  1305.   while (tags)
  1306.     {
  1307.       /* The TREE_PURPOSE of an enum tag (which becomes a member of the
  1308.      enclosing class) is set to the name for the enum type.  So, if
  1309.      inner_name is `bar', and we strike `baz' for `enum bar { baz }',
  1310.      then this test will be true.  */
  1311.       if (TREE_PURPOSE (tags) == inner_name)
  1312.     {
  1313.       if (inner_types == NULL_TREE)
  1314.         return DECL_NESTED_TYPENAME (TYPE_NAME (TREE_VALUE (tags)));
  1315.       return resolve_scope_to_name (TREE_VALUE (tags), inner_types);
  1316.     }
  1317.       tags = TREE_CHAIN (tags);
  1318.     }
  1319.  
  1320. #if 0
  1321.   /* XXX This needs to be fixed better.  */
  1322.   if (TREE_CODE (type) == UNINSTANTIATED_P_TYPE)
  1323.     {
  1324.       sorry ("nested class lookup in template type");
  1325.       return NULL_TREE;
  1326.     }
  1327. #endif
  1328.  
  1329.   /* Look for a TYPE_DECL.  */
  1330.   for (tags = TYPE_FIELDS (type); tags; tags = TREE_CHAIN (tags))
  1331.     if (TREE_CODE (tags) == TYPE_DECL && DECL_NAME (tags) == inner_name)
  1332.       {
  1333.     /* Code by raeburn.  */
  1334.     if (inner_types == NULL_TREE)
  1335.       return DECL_NESTED_TYPENAME (tags);
  1336.     return resolve_scope_to_name (TREE_TYPE (tags), inner_types);
  1337.       }
  1338.  
  1339.   return NULL_TREE;
  1340. }
  1341.  
  1342. /* Resolve an expression NAME1::NAME2::...::NAMEn to
  1343.    the name that names the above nested type.  INNER_TYPES
  1344.    is a chain of nested type names (held together by SCOPE_REFs);
  1345.    OUTER_TYPE is the type we know to enclose INNER_TYPES.
  1346.    Returns NULL_TREE if there is an error.  */
  1347. tree
  1348. resolve_scope_to_name (outer_type, inner_stuff)
  1349.      tree outer_type, inner_stuff;
  1350. {
  1351.   register tree tmp;
  1352.   tree inner_name, inner_type;
  1353.  
  1354.   if (outer_type == NULL_TREE && current_class_type != NULL_TREE)
  1355.     {
  1356.       /* We first try to look for a nesting in our current class context,
  1357.          then try any enclosing classes.  */
  1358.       tree type = current_class_type;
  1359.       
  1360.       while (type && (TREE_CODE (type) == RECORD_TYPE
  1361.               || TREE_CODE (type) == UNION_TYPE))
  1362.         {
  1363.           tree rval = resolve_scope_to_name (type, inner_stuff);
  1364.  
  1365.       if (rval != NULL_TREE)
  1366.         return rval;
  1367.       type = DECL_CONTEXT (TYPE_NAME (type));
  1368.     }
  1369.     }
  1370.  
  1371.   if (TREE_CODE (inner_stuff) == SCOPE_REF)
  1372.     {
  1373.       inner_name = TREE_OPERAND (inner_stuff, 0);
  1374.       inner_type = TREE_OPERAND (inner_stuff, 1);
  1375.     }
  1376.   else
  1377.     {
  1378.       inner_name = inner_stuff;
  1379.       inner_type = NULL_TREE;
  1380.     }
  1381.  
  1382.   if (outer_type == NULL_TREE)
  1383.     {
  1384.       tree x;
  1385.       /* If we have something that's already a type by itself,
  1386.      use that.  */
  1387.       if (IDENTIFIER_HAS_TYPE_VALUE (inner_name))
  1388.     {
  1389.       if (inner_type)
  1390.         return resolve_scope_to_name (IDENTIFIER_TYPE_VALUE (inner_name),
  1391.                       inner_type);
  1392.       return inner_name;
  1393.     }
  1394.       
  1395.       x = lookup_name (inner_name, 0);
  1396.  
  1397.       if (x && TREE_CODE (x) == NAMESPACE_DECL)
  1398.     {
  1399.       x = lookup_namespace_name (x, inner_type);
  1400.       return x;
  1401.     }
  1402.       return NULL_TREE;
  1403.     }
  1404.  
  1405.   if (! IS_AGGR_TYPE (outer_type))
  1406.     return NULL_TREE;
  1407.  
  1408.   /* Look for member classes or enums.  */
  1409.   tmp = find_scoped_type (outer_type, inner_name, inner_type);
  1410.  
  1411.   /* If it's not a type in this class, then go down into the
  1412.      base classes and search there.  */
  1413.   if (! tmp && TYPE_BINFO (outer_type))
  1414.     {
  1415.       tree binfos = TYPE_BINFO_BASETYPES (outer_type);
  1416.       int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  1417.  
  1418.       for (i = 0; i < n_baselinks; i++)
  1419.     {
  1420.       tree base_binfo = TREE_VEC_ELT (binfos, i);
  1421.       tmp = resolve_scope_to_name (BINFO_TYPE (base_binfo), inner_stuff);
  1422.       if (tmp)
  1423.         return tmp;
  1424.     }
  1425.       tmp = NULL_TREE;
  1426.     }
  1427.  
  1428.   return tmp;
  1429. }
  1430.  
  1431. /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
  1432.    This is how virtual function calls are avoided.  */
  1433. tree
  1434. build_scoped_method_call (exp, scopes, name, parms)
  1435.      tree exp, scopes, name, parms;
  1436. {
  1437.   /* Because this syntactic form does not allow
  1438.      a pointer to a base class to be `stolen',
  1439.      we need not protect the derived->base conversion
  1440.      that happens here.
  1441.      
  1442.      @@ But we do have to check access privileges later.  */
  1443.   tree basename = resolve_scope_to_name (NULL_TREE, scopes);
  1444.   tree basetype, binfo, decl;
  1445.   tree type = TREE_TYPE (exp);
  1446.  
  1447.   if (type == error_mark_node
  1448.       || basename == NULL_TREE)
  1449.     return error_mark_node;
  1450.  
  1451.   basetype = IDENTIFIER_TYPE_VALUE (basename);
  1452.  
  1453.   if (TREE_CODE (type) == REFERENCE_TYPE)
  1454.     type = TREE_TYPE (type);
  1455.  
  1456.   /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
  1457.      that explicit ~int is caught in the parser; this deals with typedefs
  1458.      and template parms.  */
  1459.   if (TREE_CODE (name) == BIT_NOT_EXPR && ! is_aggr_typedef (basename, 0))
  1460.     {
  1461.       if (type != basetype)
  1462.     cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
  1463.           exp, basetype, type);
  1464.       name = TREE_OPERAND (name, 0);
  1465.       if (basetype != get_type_value (name))
  1466.     cp_error ("qualified type `%T' does not match destructor name `~%T'",
  1467.           basetype, name);
  1468.       return convert (void_type_node, exp);
  1469.     }
  1470.  
  1471.   if (! is_aggr_typedef (basename, 1))
  1472.     return error_mark_node;
  1473.  
  1474.   if (! IS_AGGR_TYPE (type))
  1475.     {
  1476.       cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
  1477.         exp, type);
  1478.       return error_mark_node;
  1479.     }
  1480.  
  1481.   if ((binfo = binfo_or_else (basetype, type)))
  1482.     {
  1483.       if (binfo == error_mark_node)
  1484.     return error_mark_node;
  1485.       if (TREE_CODE (exp) == INDIRECT_REF)
  1486.     decl = build_indirect_ref (convert_pointer_to (binfo,
  1487.                                build_unary_op (ADDR_EXPR, exp, 0)), NULL_PTR);
  1488.       else
  1489.     decl = build_scoped_ref (exp, scopes);
  1490.  
  1491.       /* Call to a destructor.  */
  1492.       if (TREE_CODE (name) == BIT_NOT_EXPR)
  1493.     {
  1494.       /* Explicit call to destructor.  */
  1495.       name = TREE_OPERAND (name, 0);
  1496.       if (! (name == constructor_name (TREE_TYPE (decl))
  1497.          || TREE_TYPE (decl) == get_type_value (name)))
  1498.         {
  1499.           cp_error
  1500.         ("qualified type `%T' does not match destructor name `~%T'",
  1501.          TREE_TYPE (decl), name);
  1502.           return error_mark_node;
  1503.         }
  1504.       if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
  1505.         return convert (void_type_node, exp);
  1506.       
  1507.       return build_delete (TREE_TYPE (decl), decl, integer_two_node,
  1508.                    LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
  1509.                    0);
  1510.     }
  1511.  
  1512.       /* Call to a method.  */
  1513.       return build_method_call (decl, name, parms, binfo,
  1514.                 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
  1515.     }
  1516.   return error_mark_node;
  1517. }
  1518.  
  1519. static void
  1520. print_candidates (candidates)
  1521.      tree candidates;
  1522. {
  1523.   cp_error_at ("candidates are: %D", TREE_VALUE (candidates));
  1524.   candidates = TREE_CHAIN (candidates);
  1525.  
  1526.   while (candidates)
  1527.     {
  1528.       cp_error_at ("                %D", TREE_VALUE (candidates));
  1529.       candidates = TREE_CHAIN (candidates);
  1530.     }
  1531. }
  1532.  
  1533. static void
  1534. print_n_candidates (candidates, n)
  1535.      struct candidate *candidates;
  1536.      int n;
  1537. {
  1538.   int i;
  1539.  
  1540.   cp_error_at ("candidates are: %D", candidates[0].function);
  1541.   for (i = 1; i < n; i++)
  1542.     cp_error_at ("                %D", candidates[i].function);
  1543. }
  1544.  
  1545. /* Build something of the form ptr->method (args)
  1546.    or object.method (args).  This can also build
  1547.    calls to constructors, and find friends.
  1548.  
  1549.    Member functions always take their class variable
  1550.    as a pointer.
  1551.  
  1552.    INSTANCE is a class instance.
  1553.  
  1554.    NAME is the name of the method desired, usually an IDENTIFIER_NODE.
  1555.  
  1556.    PARMS help to figure out what that NAME really refers to.
  1557.  
  1558.    BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
  1559.    down to the real instance type to use for access checking.  We need this
  1560.    information to get protected accesses correct.  This parameter is used
  1561.    by build_member_call.
  1562.  
  1563.    FLAGS is the logical disjunction of zero or more LOOKUP_
  1564.    flags.  See cp-tree.h for more info.
  1565.  
  1566.    If this is all OK, calls build_function_call with the resolved
  1567.    member function.
  1568.  
  1569.    This function must also handle being called to perform
  1570.    initialization, promotion/coercion of arguments, and
  1571.    instantiation of default parameters.
  1572.  
  1573.    Note that NAME may refer to an instance variable name.  If
  1574.    `operator()()' is defined for the type of that field, then we return
  1575.    that result.  */
  1576. tree
  1577. build_method_call (instance, name, parms, basetype_path, flags)
  1578.      tree instance, name, parms, basetype_path;
  1579.      int flags;
  1580. {
  1581.   register tree function, fntype, value_type;
  1582.   register tree basetype, save_basetype;
  1583.   register tree baselink, result, method_name, parmtypes, parm;
  1584.   tree last;
  1585.   int pass;
  1586.   enum access_type access = access_public;
  1587.  
  1588.   /* Range of cases for vtable optimization.  */
  1589.   enum vtable_needs { not_needed, maybe_needed, unneeded, needed };
  1590.   enum vtable_needs need_vtbl = not_needed;
  1591.  
  1592.   char *name_kind;
  1593.   int ever_seen = 0;
  1594.   tree instance_ptr = NULL_TREE;
  1595.   int all_virtual = flag_all_virtual;
  1596.   int static_call_context = 0;
  1597.   tree found_fns = NULL_TREE;
  1598.  
  1599.   /* Keep track of `const' and `volatile' objects.  */
  1600.   int constp, volatilep;
  1601.  
  1602. #ifdef GATHER_STATISTICS
  1603.   n_build_method_call++;
  1604. #endif
  1605.  
  1606.   if (instance == error_mark_node
  1607.       || name == error_mark_node
  1608.       || parms == error_mark_node
  1609.       || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
  1610.     return error_mark_node;
  1611.  
  1612.   /* This is the logic that magically deletes the second argument to
  1613.      operator delete, if it is not needed. */
  1614.   if (name == ansi_opname[(int) DELETE_EXPR] && list_length (parms)==2)
  1615.     {
  1616.       tree save_last = TREE_CHAIN (parms);
  1617.       tree result;
  1618.       /* get rid of unneeded argument */
  1619.       TREE_CHAIN (parms) = NULL_TREE;
  1620.       result = build_method_call (instance, name, parms, basetype_path,
  1621.                   (LOOKUP_SPECULATIVELY|flags)
  1622.                   &~LOOKUP_COMPLAIN);
  1623.       /* If it finds a match, return it. */
  1624.       if (result)
  1625.     return build_method_call (instance, name, parms, basetype_path, flags);
  1626.       /* If it doesn't work, two argument delete must work */
  1627.       TREE_CHAIN (parms) = save_last;
  1628.     }
  1629.   /* We already know whether it's needed or not for vec delete.  */
  1630.   else if (name == ansi_opname[(int) VEC_DELETE_EXPR]
  1631.        && ! TYPE_VEC_DELETE_TAKES_SIZE (TREE_TYPE (instance)))
  1632.     TREE_CHAIN (parms) = NULL_TREE;
  1633.  
  1634.   if (TREE_CODE (name) == BIT_NOT_EXPR)
  1635.     {
  1636.       flags |= LOOKUP_DESTRUCTOR;
  1637.       name = TREE_OPERAND (name, 0);
  1638.       if (parms)
  1639.     error ("destructors take no parameters");
  1640.       basetype = TREE_TYPE (instance);
  1641.       if (TREE_CODE (basetype) == REFERENCE_TYPE)
  1642.     basetype = TREE_TYPE (basetype);
  1643.       if (! ((IS_AGGR_TYPE (basetype)
  1644.           && name == constructor_name (basetype))
  1645.          || basetype == get_type_value (name)))
  1646.     {
  1647.       cp_error ("destructor name `~%D' does not match type `%T' of expression",
  1648.             name, basetype);
  1649.       return convert (void_type_node, instance);
  1650.     }
  1651.  
  1652.       if (! TYPE_HAS_DESTRUCTOR (basetype))
  1653.     return convert (void_type_node, instance);
  1654.       instance = default_conversion (instance);
  1655.       instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
  1656.       return build_delete (build_pointer_type (basetype),
  1657.                instance_ptr, integer_two_node,
  1658.                LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
  1659.     }
  1660.  
  1661.   {
  1662.     char *xref_name;
  1663.     
  1664.     /* Initialize name for error reporting.  */
  1665.     if (IDENTIFIER_OPNAME_P (name) && ! IDENTIFIER_TYPENAME_P (name))
  1666.       {
  1667.     char *p = operator_name_string (name);
  1668.     xref_name = (char *)alloca (strlen (p) + 10);
  1669.     sprintf (xref_name, "operator %s", p);
  1670.       }
  1671.     else if (TREE_CODE (name) == SCOPE_REF)
  1672.       xref_name = IDENTIFIER_POINTER (TREE_OPERAND (name, 1));
  1673.     else
  1674.       xref_name = IDENTIFIER_POINTER (name);
  1675.  
  1676.     GNU_xref_call (current_function_decl, xref_name);
  1677.   }
  1678.  
  1679.   if (instance == NULL_TREE)
  1680.     {
  1681.       basetype = NULL_TREE;
  1682.       /* Check cases where this is really a call to raise
  1683.      an exception.  */
  1684.       if (current_class_type && TREE_CODE (name) == IDENTIFIER_NODE)
  1685.     {
  1686.       basetype = purpose_member (name, CLASSTYPE_TAGS (current_class_type));
  1687.       if (basetype)
  1688.         basetype = TREE_VALUE (basetype);
  1689.     }
  1690.       else if (TREE_CODE (name) == SCOPE_REF
  1691.            && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
  1692.     {
  1693.       if (! is_aggr_typedef (TREE_OPERAND (name, 0), 1))
  1694.         return error_mark_node;
  1695.       basetype = purpose_member (TREE_OPERAND (name, 1),
  1696.                      CLASSTYPE_TAGS (IDENTIFIER_TYPE_VALUE (TREE_OPERAND (name, 0))));
  1697.       if (basetype)
  1698.         basetype = TREE_VALUE (basetype);
  1699.     }
  1700.  
  1701.       if (basetype != NULL_TREE)
  1702.     ;
  1703.       /* call to a constructor... */
  1704.       else if (basetype_path)
  1705.     basetype = BINFO_TYPE (basetype_path);
  1706.       else if (IDENTIFIER_HAS_TYPE_VALUE (name))
  1707.     {
  1708.       basetype = IDENTIFIER_TYPE_VALUE (name);
  1709.       name = constructor_name_full (basetype);
  1710.     }
  1711.       else
  1712.     {
  1713.       tree typedef_name = lookup_name (name, 1);
  1714.       if (typedef_name && TREE_CODE (typedef_name) == TYPE_DECL)
  1715.         {
  1716.           /* Canonicalize the typedef name.  */
  1717.           basetype = TREE_TYPE (typedef_name);
  1718.           name = TYPE_IDENTIFIER (basetype);
  1719.         }
  1720.       else
  1721.         {
  1722.           cp_error ("no constructor named `%T' in scope",
  1723.             name);
  1724.           return error_mark_node;
  1725.         }
  1726.     }
  1727.  
  1728.       if (! IS_AGGR_TYPE (basetype))
  1729.     {
  1730.     non_aggr_error:
  1731.       if ((flags & LOOKUP_COMPLAIN) && TREE_CODE (basetype) != ERROR_MARK)
  1732.         cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
  1733.               name, instance, basetype);
  1734.  
  1735.       return error_mark_node;
  1736.     }
  1737.     }
  1738.   else if (instance == C_C_D || instance == current_class_decl)
  1739.     {
  1740.       /* When doing initialization, we side-effect the TREE_TYPE of
  1741.      C_C_D, hence we cannot set up BASETYPE from CURRENT_CLASS_TYPE.  */
  1742.       basetype = TREE_TYPE (C_C_D);
  1743.  
  1744.       /* Anything manifestly `this' in constructors and destructors
  1745.      has a known type, so virtual function tables are not needed.  */
  1746.       if (TYPE_VIRTUAL_P (basetype)
  1747.       && !(flags & LOOKUP_NONVIRTUAL))
  1748.     need_vtbl = (dtor_label || ctor_label)
  1749.       ? unneeded : maybe_needed;
  1750.  
  1751.       /* If `this' is a signature pointer and `name' is not a constructor,
  1752.      we are calling a signature member function.  In that case, set the
  1753.      `basetype' to the signature type and dereference the `optr' field.  */
  1754.       if (IS_SIGNATURE_POINTER (basetype)
  1755.       && TYPE_IDENTIFIER (basetype) != name)
  1756.     {
  1757.       basetype = SIGNATURE_TYPE (basetype);
  1758.       instance_ptr = build_optr_ref (instance);
  1759.       instance_ptr = convert (TYPE_POINTER_TO (basetype), instance_ptr);
  1760.       basetype_path = TYPE_BINFO (basetype);
  1761.     }
  1762.       else
  1763.     {
  1764.       instance = C_C_D;
  1765.       instance_ptr = current_class_decl;
  1766.       basetype_path = TYPE_BINFO (current_class_type);
  1767.     }
  1768.       result = build_field_call (basetype_path, instance_ptr, name, parms);
  1769.  
  1770.       if (result)
  1771.     return result;
  1772.     }
  1773.   else if (TREE_CODE (instance) == RESULT_DECL)
  1774.     {
  1775.       basetype = TREE_TYPE (instance);
  1776.       /* Should we ever have to make a virtual function reference
  1777.      from a RESULT_DECL, know that it must be of fixed type
  1778.      within the scope of this function.  */
  1779.       if (!(flags & LOOKUP_NONVIRTUAL) && TYPE_VIRTUAL_P (basetype))
  1780.     need_vtbl = maybe_needed;
  1781.       instance_ptr = build1 (ADDR_EXPR, TYPE_POINTER_TO (basetype), instance);
  1782.     }
  1783.   else
  1784.     {
  1785.       /* The MAIN_VARIANT of the type that `instance_ptr' winds up being.  */
  1786.       tree inst_ptr_basetype;
  1787.  
  1788.       static_call_context =
  1789.     (TREE_CODE (instance) == INDIRECT_REF
  1790.      && TREE_CODE (TREE_OPERAND (instance, 0)) == NOP_EXPR
  1791.      && TREE_OPERAND (TREE_OPERAND (instance, 0), 0) == error_mark_node);
  1792.  
  1793.       if (TREE_CODE (instance) == OFFSET_REF)
  1794.     instance = resolve_offset_ref (instance);
  1795.  
  1796.       /* the base type of an instance variable is pointer to class */
  1797.       basetype = TREE_TYPE (instance);
  1798.  
  1799.       if (TREE_CODE (basetype) == REFERENCE_TYPE)
  1800.     {
  1801.       basetype = TREE_TYPE (basetype);
  1802.       if (! IS_AGGR_TYPE (basetype))
  1803.         goto non_aggr_error;
  1804.       /* Call to convert not needed because we are remaining
  1805.          within the same type.  */
  1806.       instance_ptr = build1 (NOP_EXPR, build_pointer_type (basetype),
  1807.                  instance);
  1808.       inst_ptr_basetype = TYPE_MAIN_VARIANT (basetype);
  1809.     }
  1810.       else
  1811.     {
  1812.       if (! IS_AGGR_TYPE (basetype)
  1813.           && ! (TYPE_LANG_SPECIFIC (basetype)
  1814.             && (IS_SIGNATURE_POINTER (basetype)
  1815.             || IS_SIGNATURE_REFERENCE (basetype))))
  1816.         goto non_aggr_error;
  1817.  
  1818.       /* If `instance' is a signature pointer/reference and `name' is
  1819.          not a constructor, we are calling a signature member function.
  1820.          In that case set the `basetype' to the signature type.  */
  1821.       if ((IS_SIGNATURE_POINTER (basetype)
  1822.            || IS_SIGNATURE_REFERENCE (basetype))
  1823.           && TYPE_IDENTIFIER (basetype) != name)
  1824.         basetype = SIGNATURE_TYPE (basetype);
  1825.  
  1826.       if ((IS_SIGNATURE (basetype)
  1827.            && (instance_ptr = build_optr_ref (instance)))
  1828.           || (lvalue_p (instance)
  1829.           && (instance_ptr = build_unary_op (ADDR_EXPR, instance, 0)))
  1830.           || (instance_ptr = unary_complex_lvalue (ADDR_EXPR, instance)))
  1831.         {
  1832.           if (instance_ptr == error_mark_node)
  1833.         return error_mark_node;
  1834.         }
  1835.       else if (TREE_CODE (instance) == NOP_EXPR
  1836.            || TREE_CODE (instance) == CONSTRUCTOR)
  1837.         {
  1838.           /* A cast is not an lvalue.  Initialize a fresh temp
  1839.          with the value we are casting from, and proceed with
  1840.          that temporary.  We can't cast to a reference type,
  1841.          so that simplifies the initialization to something
  1842.          we can manage.  */
  1843.           tree temp = get_temp_name (TREE_TYPE (instance), 0);
  1844.           if (IS_AGGR_TYPE (TREE_TYPE (instance)))
  1845.         expand_aggr_init (temp, instance, 0, flags);
  1846.           else
  1847.         {
  1848.           store_init_value (temp, instance);
  1849.           expand_decl_init (temp);
  1850.         }
  1851.           instance = temp;
  1852.           instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
  1853.         }
  1854.       else
  1855.         {
  1856.           if (TREE_CODE (instance) != CALL_EXPR)
  1857.         my_friendly_abort (125);
  1858.           if (TYPE_NEEDS_CONSTRUCTING (basetype))
  1859.         instance = build_cplus_new (basetype, instance, 0);
  1860.           else
  1861.         {
  1862.           instance = get_temp_name (basetype, 0);
  1863.           TREE_ADDRESSABLE (instance) = 1;
  1864.         }
  1865.           instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
  1866.         }
  1867.       /* @@ Should we call comp_target_types here?  */
  1868.       inst_ptr_basetype = TREE_TYPE (TREE_TYPE (instance_ptr));
  1869.       if (TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (inst_ptr_basetype))
  1870.         basetype = inst_ptr_basetype;
  1871.       else
  1872.         {
  1873.           instance_ptr = convert (TYPE_POINTER_TO (basetype), instance_ptr);
  1874.           if (instance_ptr == error_mark_node)
  1875.         return error_mark_node;
  1876.         }
  1877.     }
  1878.  
  1879.       /* After converting `instance_ptr' above, `inst_ptr_basetype' was
  1880.      not updated, so we use `basetype' instead.  */
  1881.       if (basetype_path == NULL_TREE
  1882.       && IS_SIGNATURE (basetype))
  1883.     basetype_path = TYPE_BINFO (basetype);
  1884.       else if (basetype_path == NULL_TREE ||
  1885.     BINFO_TYPE (basetype_path) != TYPE_MAIN_VARIANT (inst_ptr_basetype))
  1886.     basetype_path = TYPE_BINFO (inst_ptr_basetype);
  1887.  
  1888.       result = build_field_call (basetype_path, instance_ptr, name, parms);
  1889.       if (result)
  1890.     return result;
  1891.  
  1892.       if (!(flags & LOOKUP_NONVIRTUAL) && TYPE_VIRTUAL_P (basetype))
  1893.     {
  1894.       if (TREE_SIDE_EFFECTS (instance_ptr))
  1895.         {
  1896.           /* This action is needed because the instance is needed
  1897.          for providing the base of the virtual function table.
  1898.          Without using a SAVE_EXPR, the function we are building
  1899.          may be called twice, or side effects on the instance
  1900.          variable (such as a post-increment), may happen twice.  */
  1901.           instance_ptr = save_expr (instance_ptr);
  1902.           instance = build_indirect_ref (instance_ptr, NULL_PTR);
  1903.         }
  1904.       else if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
  1905.         {
  1906.           /* This happens when called for operator new ().  */
  1907.           instance = build_indirect_ref (instance, NULL_PTR);
  1908.         }
  1909.  
  1910.       need_vtbl = maybe_needed;
  1911.     }
  1912.     }
  1913.  
  1914.   if (TYPE_SIZE (basetype) == 0)
  1915.     {
  1916.       /* This is worth complaining about, I think.  */
  1917.       cp_error ("cannot lookup method in incomplete type `%T'", basetype);
  1918.       return error_mark_node;
  1919.     }
  1920.  
  1921.   save_basetype = TYPE_MAIN_VARIANT (basetype);
  1922.  
  1923. #if 0
  1924.   if (all_virtual == 1
  1925.       && (! strncmp (IDENTIFIER_POINTER (name), OPERATOR_METHOD_FORMAT,
  1926.              OPERATOR_METHOD_LENGTH)
  1927.       || instance_ptr == NULL_TREE
  1928.       || (TYPE_OVERLOADS_METHOD_CALL_EXPR (basetype) == 0)))
  1929.     all_virtual = 0;
  1930. #endif
  1931.  
  1932.   last = NULL_TREE;
  1933.   for (parmtypes = NULL_TREE, parm = parms; parm; parm = TREE_CHAIN (parm))
  1934.     {
  1935.       tree t = TREE_TYPE (TREE_VALUE (parm));
  1936.       if (TREE_CODE (t) == OFFSET_TYPE)
  1937.     {
  1938.       /* Convert OFFSET_TYPE entities to their normal selves.  */
  1939.       TREE_VALUE (parm) = resolve_offset_ref (TREE_VALUE (parm));
  1940.       t = TREE_TYPE (TREE_VALUE (parm));
  1941.     }
  1942.       if (TREE_CODE (TREE_VALUE (parm)) == OFFSET_REF
  1943.       && TREE_CODE (t) == METHOD_TYPE)
  1944.     {
  1945.       TREE_VALUE (parm) = build_unary_op (ADDR_EXPR, TREE_VALUE (parm), 0);
  1946.     }
  1947. #if 0
  1948.       /* This breaks reference-to-array parameters.  */
  1949.       if (TREE_CODE (t) == ARRAY_TYPE)
  1950.     {
  1951.       /* Perform the conversion from ARRAY_TYPE to POINTER_TYPE in place.
  1952.          This eliminates needless calls to `compute_conversion_costs'.  */
  1953.       TREE_VALUE (parm) = default_conversion (TREE_VALUE (parm));
  1954.       t = TREE_TYPE (TREE_VALUE (parm));
  1955.     }
  1956. #endif
  1957.       if (t == error_mark_node)
  1958.     return error_mark_node;
  1959.       last = build_tree_list (NULL_TREE, t);
  1960.       parmtypes = chainon (parmtypes, last);
  1961.     }
  1962.  
  1963.   if (instance)
  1964.     {
  1965.       /* TREE_READONLY (instance) fails for references.  */
  1966.       constp = TYPE_READONLY (TREE_TYPE (TREE_TYPE (instance_ptr)));
  1967.       volatilep = TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (instance_ptr)));
  1968.       parms = tree_cons (NULL_TREE, instance_ptr, parms);
  1969.     }
  1970.   else
  1971.     {
  1972.       /* Raw constructors are always in charge.  */
  1973.       if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)
  1974.       && ! (flags & LOOKUP_HAS_IN_CHARGE))
  1975.     {
  1976.       flags |= LOOKUP_HAS_IN_CHARGE;
  1977.       parms = tree_cons (NULL_TREE, integer_one_node, parms);
  1978.       parmtypes = tree_cons (NULL_TREE, integer_type_node, parmtypes);
  1979.     }
  1980.  
  1981.       if (flag_this_is_variable > 0)
  1982.     {
  1983.       constp = 0;
  1984.       volatilep = 0;
  1985.       instance_ptr = build_int_2 (0, 0);
  1986.       TREE_TYPE (instance_ptr) = TYPE_POINTER_TO (basetype);
  1987.       parms = tree_cons (NULL_TREE, instance_ptr, parms);
  1988.     }
  1989.       else
  1990.     {
  1991.       constp = 0;
  1992.       volatilep = 0;
  1993.       instance_ptr = build_new (NULL_TREE, basetype, void_type_node, 0);
  1994.       if (instance_ptr == error_mark_node)
  1995.         return error_mark_node;
  1996.       instance_ptr = save_expr (instance_ptr);
  1997.       TREE_CALLS_NEW (instance_ptr) = 1;
  1998.       instance = build_indirect_ref (instance_ptr, NULL_PTR);
  1999.  
  2000. #if 0
  2001.       /* This breaks initialization of a reference from a new
  2002.              expression of a different type.  And it doesn't appear to
  2003.              serve its original purpose any more, either.  jason 10/12/94 */
  2004.       /* If it's a default argument initialized from a ctor, what we get
  2005.          from instance_ptr will match the arglist for the FUNCTION_DECL
  2006.          of the constructor.  */
  2007.       if (parms && TREE_CODE (TREE_VALUE (parms)) == CALL_EXPR
  2008.           && TREE_OPERAND (TREE_VALUE (parms), 1)
  2009.           && TREE_CALLS_NEW (TREE_VALUE (TREE_OPERAND (TREE_VALUE (parms), 1))))
  2010.         parms = build_tree_list (NULL_TREE, instance_ptr);
  2011.       else
  2012. #endif
  2013.         parms = tree_cons (NULL_TREE, instance_ptr, parms);
  2014.     }
  2015.     }
  2016.  
  2017.   parmtypes = tree_cons (NULL_TREE, TREE_TYPE (instance_ptr), parmtypes);
  2018.  
  2019.   if (last == NULL_TREE)
  2020.     last = parmtypes;
  2021.  
  2022.   /* Look up function name in the structure type definition.  */
  2023.  
  2024.   if ((IDENTIFIER_HAS_TYPE_VALUE (name)
  2025.        && ! IDENTIFIER_OPNAME_P (name)
  2026.        && IS_AGGR_TYPE (IDENTIFIER_TYPE_VALUE (name))
  2027.        && TREE_CODE (IDENTIFIER_TYPE_VALUE (name)) != UNINSTANTIATED_P_TYPE)
  2028.       || name == constructor_name (basetype))
  2029.     {
  2030.       tree tmp = NULL_TREE;
  2031.       if (IDENTIFIER_TYPE_VALUE (name) == basetype
  2032.       || name == constructor_name (basetype))
  2033.     tmp = TYPE_BINFO (basetype);
  2034.       else
  2035.     tmp = get_binfo (IDENTIFIER_TYPE_VALUE (name), basetype, 0);
  2036.       
  2037.       if (tmp != NULL_TREE)
  2038.     {
  2039.       name_kind = "constructor";
  2040.       
  2041.       if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)
  2042.           && ! (flags & LOOKUP_HAS_IN_CHARGE))
  2043.         {
  2044.           /* Constructors called for initialization
  2045.          only are never in charge.  */
  2046.           tree tmplist;
  2047.           
  2048.           flags |= LOOKUP_HAS_IN_CHARGE;
  2049.           tmplist = tree_cons (NULL_TREE, integer_zero_node,
  2050.                    TREE_CHAIN (parms));
  2051.           TREE_CHAIN (parms) = tmplist;
  2052.           tmplist = tree_cons (NULL_TREE, integer_type_node, TREE_CHAIN (parmtypes));
  2053.           TREE_CHAIN (parmtypes) = tmplist;
  2054.         }
  2055.       basetype = BINFO_TYPE (tmp);
  2056.     }
  2057.       else
  2058.     name_kind = "method";
  2059.     }
  2060.   else
  2061.     name_kind = "method";
  2062.   
  2063.   if (basetype_path == NULL_TREE
  2064.       || BINFO_TYPE (basetype_path) != TYPE_MAIN_VARIANT (basetype))
  2065.     basetype_path = TYPE_BINFO (basetype);
  2066.   result = lookup_fnfields (basetype_path, name,
  2067.                 (flags & LOOKUP_COMPLAIN));
  2068.   if (result == error_mark_node)
  2069.     return error_mark_node;
  2070.  
  2071.  
  2072. #if 0
  2073.   /* Now, go look for this method name.  We do not find destructors here.
  2074.  
  2075.      Putting `void_list_node' on the end of the parmtypes
  2076.      fakes out `build_decl_overload' into doing the right thing.  */
  2077.   TREE_CHAIN (last) = void_list_node;
  2078.   method_name = build_decl_overload (name, parmtypes,
  2079.                      1 + (name == constructor_name (save_basetype)
  2080.                       || name == constructor_name_full (save_basetype)));
  2081.   TREE_CHAIN (last) = NULL_TREE;
  2082. #endif
  2083.  
  2084.   for (pass = 0; pass < 2; pass++)
  2085.     {
  2086.       struct candidate *candidates;
  2087.       struct candidate *cp;
  2088.       int len;
  2089.       unsigned best = 1;
  2090.  
  2091.       /* This increments every time we go up the type hierarchy.
  2092.      The idea is to prefer a function of the derived class if possible. */
  2093.       int b_or_d = 0;
  2094.  
  2095.       baselink = result;
  2096.  
  2097.       if (pass > 0)
  2098.     {
  2099.       candidates
  2100.         = (struct candidate *) alloca ((ever_seen+1)
  2101.                        * sizeof (struct candidate));
  2102.       bzero ((char *) candidates, (ever_seen + 1) * sizeof (struct candidate));
  2103.       cp = candidates;
  2104.       len = list_length (parms);
  2105.       ever_seen = 0;
  2106.  
  2107.       /* First see if a global function has a shot at it.  */
  2108.       if (flags & LOOKUP_GLOBAL)
  2109.         {
  2110.           tree friend_parms;
  2111.           tree parm = instance_ptr;
  2112.  
  2113.           if (TREE_CODE (TREE_TYPE (parm)) == REFERENCE_TYPE)
  2114.         parm = convert_from_reference (parm);
  2115.           else if (TREE_CODE (TREE_TYPE (parm)) == POINTER_TYPE)
  2116.         parm = build_indirect_ref (parm, "friendifying parms (compiler error)");
  2117.           else
  2118.         my_friendly_abort (167);
  2119.  
  2120.           friend_parms = tree_cons (NULL_TREE, parm, TREE_CHAIN (parms));
  2121.  
  2122.           cp->h_len = len;
  2123.           cp->harshness = (struct harshness_code *)
  2124.         alloca ((len + 1) * sizeof (struct harshness_code));
  2125.  
  2126.           result = build_overload_call (name, friend_parms, 0, cp);
  2127.           /* If it turns out to be the one we were actually looking for
  2128.          (it was probably a friend function), the return the
  2129.          good result.  */
  2130.           if (TREE_CODE (result) == CALL_EXPR)
  2131.         return result;
  2132.  
  2133.           while ((cp->h.code & EVIL_CODE) == 0)
  2134.         {
  2135.           /* non-standard uses: set the field to 0 to indicate
  2136.              we are using a non-member function.  */
  2137.           cp->u.field = 0;
  2138.           if (cp->harshness[len].distance == 0
  2139.               && cp->h.code < best)
  2140.             best = cp->h.code;
  2141.           cp += 1;
  2142.         }
  2143.         }
  2144.     }
  2145.  
  2146.       while (baselink)
  2147.     {
  2148.       /* We have a hit (of sorts). If the parameter list is
  2149.          "error_mark_node", or some variant thereof, it won't
  2150.          match any methods.  Since we have verified that the is
  2151.          some method vaguely matching this one (in name at least),
  2152.          silently return.
  2153.          
  2154.          Don't stop for friends, however.  */
  2155.       basetype_path = TREE_PURPOSE (baselink);
  2156.  
  2157.       function = TREE_VALUE (baselink);
  2158.       if (TREE_CODE (basetype_path) == TREE_LIST)
  2159.         basetype_path = TREE_VALUE (basetype_path);
  2160.       basetype = BINFO_TYPE (basetype_path);
  2161.  
  2162. #if 0
  2163.       /* Cast the instance variable if necessary.  */
  2164.       if (basetype != TYPE_MAIN_VARIANT
  2165.           (TREE_TYPE (TREE_TYPE (TREE_VALUE (parms)))))
  2166.         {
  2167.           if (basetype == save_basetype)
  2168.         TREE_VALUE (parms) = instance_ptr;
  2169.           else
  2170.         {
  2171.           tree type = build_pointer_type
  2172.             (build_type_variant (basetype, constp, volatilep));
  2173.           TREE_VALUE (parms) = convert_force (type, instance_ptr, 0);
  2174.         }
  2175.         }
  2176.  
  2177.       /* FIXME: this is the wrong place to get an error.  Hopefully
  2178.          the access-control rewrite will make this change more cleanly.  */
  2179.       if (TREE_VALUE (parms) == error_mark_node)
  2180.         return error_mark_node;
  2181. #endif
  2182.  
  2183.       if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (function)))
  2184.         function = DECL_CHAIN (function);
  2185.  
  2186.       for (; function; function = DECL_CHAIN (function))
  2187.         {
  2188. #ifdef GATHER_STATISTICS
  2189.           n_inner_fields_searched++;
  2190. #endif
  2191.           ever_seen++;
  2192.           if (pass > 0)
  2193.         found_fns = tree_cons (NULL_TREE, function, found_fns);
  2194.  
  2195.           /* Not looking for friends here.  */
  2196.           if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE
  2197.           && ! DECL_STATIC_FUNCTION_P (function))
  2198.         continue;
  2199.  
  2200. #if 0
  2201.           if (pass == 0
  2202.           && DECL_ASSEMBLER_NAME (function) == method_name)
  2203.         goto found;
  2204. #endif
  2205.  
  2206.           if (pass > 0)
  2207.         {
  2208.           tree these_parms = parms;
  2209.  
  2210. #ifdef GATHER_STATISTICS
  2211.           n_inner_fields_searched++;
  2212. #endif
  2213.           cp->h_len = len;
  2214.           cp->harshness = (struct harshness_code *)
  2215.             alloca ((len + 1) * sizeof (struct harshness_code));
  2216.  
  2217.           if (DECL_STATIC_FUNCTION_P (function))
  2218.             these_parms = TREE_CHAIN (these_parms);
  2219.           compute_conversion_costs (function, these_parms, cp, len);
  2220.  
  2221.           if ((cp->h.code & EVIL_CODE) == 0)
  2222.             {
  2223.               cp->u.field = function;
  2224.               cp->function = function;
  2225.               cp->basetypes = basetype_path;
  2226.  
  2227.               /* Don't allow non-converting constructors to convert. */
  2228.               if (flags & LOOKUP_ONLYCONVERTING
  2229.               && DECL_LANG_SPECIFIC (function)
  2230.               && DECL_NONCONVERTING_P (function))
  2231.             continue;
  2232.  
  2233.               /* No "two-level" conversions.  */
  2234.               if (flags & LOOKUP_NO_CONVERSION
  2235.               && (cp->h.code & USER_CODE))
  2236.             continue;
  2237.  
  2238.               cp++;
  2239.             }
  2240.         }
  2241.         }
  2242.       /* Now we have run through one link's member functions.
  2243.          arrange to head-insert this link's links.  */
  2244.       baselink = next_baselink (baselink);
  2245.       b_or_d += 1;
  2246.       /* Don't grab functions from base classes.  lookup_fnfield will
  2247.          do the work to get us down into the right place.  */
  2248.       baselink = NULL_TREE;
  2249.     }
  2250.       if (pass == 0)
  2251.     {
  2252.       tree igv = lookup_name_nonclass (name);
  2253.  
  2254.       /* No exact match could be found.  Now try to find match
  2255.          using default conversions.  */
  2256.       if ((flags & LOOKUP_GLOBAL) && igv)
  2257.         {
  2258.           if (TREE_CODE (igv) == FUNCTION_DECL)
  2259.         ever_seen += 1;
  2260.           else if (TREE_CODE (igv) == TREE_LIST)
  2261.         ever_seen += count_functions (igv);
  2262.         }
  2263.  
  2264.       if (ever_seen == 0)
  2265.         {
  2266.           if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
  2267.           == LOOKUP_SPECULATIVELY)
  2268.         return NULL_TREE;
  2269.           
  2270.           TREE_CHAIN (last) = void_list_node;
  2271.           if (flags & LOOKUP_GLOBAL)
  2272.         cp_error ("no global or member function `%D(%A)' defined",
  2273.               name, parmtypes);
  2274.           else
  2275.         cp_error ("no member function `%T::%D(%A)' defined",
  2276.               save_basetype, name, TREE_CHAIN (parmtypes));
  2277.           return error_mark_node;
  2278.         }
  2279.       continue;
  2280.     }
  2281.  
  2282.       if (cp - candidates != 0)
  2283.     {
  2284.       /* Rank from worst to best.  Then cp will point to best one.
  2285.          Private fields have their bits flipped.  For unsigned
  2286.          numbers, this should make them look very large.
  2287.          If the best alternate has a (signed) negative value,
  2288.          then all we ever saw were private members.  */
  2289.       if (cp - candidates > 1)
  2290.         {
  2291.           int n_candidates = cp - candidates;
  2292.           extern int warn_synth;
  2293.           TREE_VALUE (parms) = instance_ptr;
  2294.           cp = ideal_candidate (save_basetype, candidates,
  2295.                     n_candidates, parms, len);
  2296.           if (cp == (struct candidate *)0)
  2297.         {
  2298.           if (flags & LOOKUP_COMPLAIN)
  2299.             {
  2300.               TREE_CHAIN (last) = void_list_node;
  2301.               cp_error ("call of overloaded %s `%D(%A)' is ambiguous",
  2302.                 name_kind, name, TREE_CHAIN (parmtypes));
  2303.               print_n_candidates (candidates, n_candidates);
  2304.             }
  2305.           return error_mark_node;
  2306.         }
  2307.           if (cp->h.code & EVIL_CODE)
  2308.         return error_mark_node;
  2309.           if (warn_synth
  2310.           && DECL_NAME (cp->function) == ansi_opname[MODIFY_EXPR]
  2311.           && DECL_ARTIFICIAL (cp->function)
  2312.           && n_candidates == 2)
  2313.         {
  2314.           cp_warning ("using synthesized `%#D' for copy assignment",
  2315.                   cp->function);
  2316.           cp_warning_at ("  where cfront would use `%#D'",
  2317.                  candidates->function);
  2318.         }
  2319.         }
  2320.       else if (cp[-1].h.code & EVIL_CODE)
  2321.         {
  2322.           if (flags & LOOKUP_COMPLAIN)
  2323.         cp_error ("ambiguous type conversion requested for %s `%D'",
  2324.               name_kind, name);
  2325.           return error_mark_node;
  2326.         }
  2327.       else
  2328.         cp--;
  2329.  
  2330.       /* The global function was the best, so use it.  */
  2331.       if (cp->u.field == 0)
  2332.         {
  2333.           /* We must convert the instance pointer into a reference type.
  2334.          Global overloaded functions can only either take
  2335.          aggregate objects (which come for free from references)
  2336.          or reference data types anyway.  */
  2337.           TREE_VALUE (parms) = copy_node (instance_ptr);
  2338.           TREE_TYPE (TREE_VALUE (parms)) = build_reference_type (TREE_TYPE (TREE_TYPE (instance_ptr)));
  2339.           return build_function_call (cp->function, parms);
  2340.         }
  2341.  
  2342.       function = cp->function;
  2343.       basetype_path = cp->basetypes;
  2344.       if (! DECL_STATIC_FUNCTION_P (function))
  2345.         TREE_VALUE (parms) = cp->arg;
  2346.       goto found_and_maybe_warn;
  2347.     }
  2348.  
  2349.       if (flags & (LOOKUP_COMPLAIN|LOOKUP_SPECULATIVELY))
  2350.     {
  2351.       if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
  2352.           == LOOKUP_SPECULATIVELY)
  2353.         return NULL_TREE;
  2354.  
  2355.       if (DECL_STATIC_FUNCTION_P (cp->function))
  2356.         parms = TREE_CHAIN (parms);
  2357.       if (ever_seen)
  2358.         {
  2359.           if (flags & LOOKUP_SPECULATIVELY)
  2360.         return NULL_TREE;
  2361.           if (static_call_context
  2362.           && TREE_CODE (TREE_TYPE (cp->function)) == METHOD_TYPE)
  2363.         cp_error ("object missing in call to `%D'", cp->function);
  2364.           else if (ever_seen > 1)
  2365.         {
  2366.           TREE_CHAIN (last) = void_list_node;
  2367.           cp_error ("no matching function for call to `%T::%D (%A)'",
  2368.                 TREE_TYPE (TREE_TYPE (instance_ptr)),
  2369.                 name, TREE_CHAIN (parmtypes));
  2370.           TREE_CHAIN (last) = NULL_TREE;
  2371.           print_candidates (found_fns);
  2372.         }
  2373.           else
  2374.         report_type_mismatch (cp, parms, name_kind);
  2375.           return error_mark_node;
  2376.         }
  2377.  
  2378.       if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
  2379.           == LOOKUP_COMPLAIN)
  2380.         {
  2381.           cp_error ("%T has no method named %D", save_basetype, name);
  2382.           return error_mark_node;
  2383.         }
  2384.       return NULL_TREE;
  2385.     }
  2386.       continue;
  2387.  
  2388.     found_and_maybe_warn:
  2389.       if ((cp->harshness[0].code & CONST_CODE)
  2390.       /* 12.1p2: Constructors can be called for const objects.  */
  2391.       && ! DECL_CONSTRUCTOR_P (cp->function))
  2392.     {
  2393.       if (flags & LOOKUP_COMPLAIN)
  2394.         {
  2395.           cp_error_at ("non-const member function `%D'", cp->function);
  2396.           error ("called for const object at this point in file");
  2397.         }
  2398.       /* Not good enough for a match.  */
  2399.       else
  2400.         return error_mark_node;
  2401.     }
  2402.       goto found;
  2403.     }
  2404.   /* Silently return error_mark_node.  */
  2405.   return error_mark_node;
  2406.  
  2407.  found:
  2408.   if (flags & LOOKUP_PROTECT)
  2409.     access = compute_access (basetype_path, function);
  2410.  
  2411.   if (access == access_private)
  2412.     {
  2413.       if (flags & LOOKUP_COMPLAIN)
  2414.     {
  2415.       cp_error_at ("%s `%+#D' is %s", name_kind, function, 
  2416.                TREE_PRIVATE (function) ? "private"
  2417.                : "from private base class");
  2418.       error ("within this context");
  2419.     }
  2420.       return error_mark_node;
  2421.     }
  2422.   else if (access == access_protected)
  2423.     {
  2424.       if (flags & LOOKUP_COMPLAIN)
  2425.     {
  2426.       cp_error_at ("%s `%+#D' %s", name_kind, function,
  2427.                TREE_PROTECTED (function) ? "is protected"
  2428.                : "has protected accessibility");
  2429.       error ("within this context");
  2430.     }
  2431.       return error_mark_node;
  2432.     }
  2433.  
  2434.   /* From here on down, BASETYPE is the type that INSTANCE_PTR's
  2435.      type (if it exists) is a pointer to.  */
  2436.  
  2437.   if (DECL_ABSTRACT_VIRTUAL_P (function)
  2438.       && instance == C_C_D
  2439.       && DECL_CONSTRUCTOR_P (current_function_decl)
  2440.       && ! (flags & LOOKUP_NONVIRTUAL)
  2441.       && value_member (function, get_abstract_virtuals (basetype)))
  2442.     cp_error ("abstract virtual `%#D' called from constructor", function);
  2443.  
  2444.   if (IS_SIGNATURE (basetype) && static_call_context)
  2445.     {
  2446.       cp_error ("cannot call signature member function `%T::%D' without signature pointer/reference",
  2447.         basetype, name);
  2448.       return error_mark_node;
  2449.     }
  2450.   else if (IS_SIGNATURE (basetype))
  2451.     return build_signature_method_call (basetype, instance, function, parms);
  2452.  
  2453.   function = DECL_MAIN_VARIANT (function);
  2454.   /* Declare external function if necessary. */
  2455.   assemble_external (function);
  2456.  
  2457. #if 1
  2458.   /* Is it a synthesized method that needs to be synthesized?  */
  2459.   if (DECL_ARTIFICIAL (function) && ! flag_no_inline
  2460.       && ! DECL_INITIAL (function)
  2461.       /* Kludge: don't synthesize for default args.  */
  2462.       && current_function_decl)
  2463.     synthesize_method (function);
  2464. #endif
  2465.  
  2466.   if (pedantic && DECL_THIS_INLINE (function) && ! DECL_ARTIFICIAL (function)
  2467.        && ! DECL_INITIAL (function) && ! DECL_PENDING_INLINE_INFO (function))
  2468.     cp_pedwarn ("inline function `%#D' called before definition", function);
  2469.  
  2470.   fntype = TREE_TYPE (function);
  2471.   if (TREE_CODE (fntype) == POINTER_TYPE)
  2472.     fntype = TREE_TYPE (fntype);
  2473.   basetype = DECL_CLASS_CONTEXT (function);
  2474.  
  2475.   /* If we are referencing a virtual function from an object
  2476.      of effectively static type, then there is no need
  2477.      to go through the virtual function table.  */
  2478.   if (need_vtbl == maybe_needed)
  2479.     {
  2480.       int fixed_type = resolves_to_fixed_type_p (instance, 0);
  2481.  
  2482.       if (all_virtual == 1
  2483.       && DECL_VINDEX (function)
  2484.       && may_be_remote (basetype))
  2485.     need_vtbl = needed;
  2486.       else if (DECL_VINDEX (function))
  2487.     need_vtbl = fixed_type ? unneeded : needed;
  2488.       else
  2489.     need_vtbl = not_needed;
  2490.     }
  2491.  
  2492.   if (TREE_CODE (fntype) == METHOD_TYPE && static_call_context
  2493.       && !DECL_CONSTRUCTOR_P (function))
  2494.     {
  2495.       /* Let's be nice to the user for now, and give reasonable
  2496.      default behavior.  */
  2497.       instance_ptr = current_class_decl;
  2498.       if (instance_ptr)
  2499.     {
  2500.       if (basetype != current_class_type)
  2501.         {
  2502.           tree binfo = get_binfo (basetype, current_class_type, 1);
  2503.           if (binfo == NULL_TREE)
  2504.         {
  2505.           error_not_base_type (function, current_class_type);
  2506.           return error_mark_node;
  2507.         }
  2508.           else if (basetype == error_mark_node)
  2509.         return error_mark_node;
  2510.         }
  2511.     }
  2512.       /* Only allow a static member function to call another static member
  2513.      function.  */
  2514.       else if (DECL_LANG_SPECIFIC (function)
  2515.            && !DECL_STATIC_FUNCTION_P (function))
  2516.     {
  2517.       cp_error ("cannot call member function `%D' without object",
  2518.             function);
  2519.       return error_mark_node;
  2520.     }
  2521.     }
  2522.  
  2523.   value_type = TREE_TYPE (fntype) ? TREE_TYPE (fntype) : void_type_node;
  2524.  
  2525.   if (TYPE_SIZE (value_type) == 0)
  2526.     {
  2527.       if (flags & LOOKUP_COMPLAIN)
  2528.     incomplete_type_error (0, value_type);
  2529.       return error_mark_node;
  2530.     }
  2531.  
  2532.   if (DECL_STATIC_FUNCTION_P (function))
  2533.     parms = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
  2534.                    TREE_CHAIN (parms), function, LOOKUP_NORMAL);
  2535.   else if (need_vtbl == unneeded)
  2536.     {
  2537.       int sub_flags = DECL_CONSTRUCTOR_P (function) ? flags : LOOKUP_NORMAL;
  2538.       basetype = TREE_TYPE (instance);
  2539.       if (TYPE_METHOD_BASETYPE (TREE_TYPE (function)) != TYPE_MAIN_VARIANT (basetype)
  2540.       && TYPE_USES_COMPLEX_INHERITANCE (basetype))
  2541.     {
  2542.       basetype = DECL_CLASS_CONTEXT (function);
  2543.       instance_ptr = convert_pointer_to (basetype, instance_ptr);
  2544.       instance = build_indirect_ref (instance_ptr, NULL_PTR);
  2545.     }
  2546.       parms = tree_cons (NULL_TREE, instance_ptr,
  2547.              convert_arguments (NULL_TREE, TREE_CHAIN (TYPE_ARG_TYPES (fntype)), TREE_CHAIN (parms), function, sub_flags));
  2548.     }
  2549.   else
  2550.     {
  2551.       if ((flags & LOOKUP_NONVIRTUAL) == 0)
  2552.     basetype = DECL_CONTEXT (function);
  2553.  
  2554.       /* First parm could be integer_zerop with casts like
  2555.      ((Object*)0)->Object::IsA()  */
  2556.       if (!integer_zerop (TREE_VALUE (parms)))
  2557.     {
  2558.       /* Since we can't have inheritance with a union, doing get_binfo
  2559.          on it won't work.  We do all the convert_pointer_to_real
  2560.          stuff to handle MI correctly...for unions, that's not
  2561.          an issue, so we must short-circuit that extra work here.  */
  2562.       tree tmp = TREE_TYPE (TREE_TYPE (TREE_VALUE (parms)));
  2563.       if (tmp != NULL_TREE && TREE_CODE (tmp) == UNION_TYPE)
  2564.         instance_ptr = TREE_VALUE (parms);
  2565.       else
  2566.         {
  2567.           tree binfo = get_binfo (basetype,
  2568.                       TREE_TYPE (TREE_TYPE (TREE_VALUE (parms))),
  2569.                       0);
  2570.           instance_ptr = convert_pointer_to_real (binfo, TREE_VALUE (parms));
  2571.         }
  2572.       instance_ptr
  2573.         = convert_pointer_to (build_type_variant (basetype,
  2574.                               constp, volatilep),
  2575.                   instance_ptr);
  2576.  
  2577.       if (TREE_CODE (instance_ptr) == COND_EXPR)
  2578.         {
  2579.           instance_ptr = save_expr (instance_ptr);
  2580.           instance = build_indirect_ref (instance_ptr, NULL_PTR);
  2581.         }
  2582.       else if (TREE_CODE (instance_ptr) == NOP_EXPR
  2583.            && TREE_CODE (TREE_OPERAND (instance_ptr, 0)) == ADDR_EXPR
  2584.            && TREE_OPERAND (TREE_OPERAND (instance_ptr, 0), 0) == instance)
  2585.         ;
  2586.       /* The call to `convert_pointer_to' may return error_mark_node.  */
  2587.       else if (TREE_CODE (instance_ptr) == ERROR_MARK)
  2588.         return instance_ptr;
  2589.       else if (instance == NULL_TREE
  2590.            || TREE_CODE (instance) != INDIRECT_REF
  2591.            || TREE_OPERAND (instance, 0) != instance_ptr)
  2592.         instance = build_indirect_ref (instance_ptr, NULL_PTR);
  2593.     }
  2594.       parms = tree_cons (NULL_TREE, instance_ptr,
  2595.              convert_arguments (NULL_TREE, TREE_CHAIN (TYPE_ARG_TYPES (fntype)), TREE_CHAIN (parms), function, LOOKUP_NORMAL));
  2596.     }
  2597.  
  2598. #if 0
  2599.   /* Constructors do not overload method calls.  */
  2600.   else if (TYPE_OVERLOADS_METHOD_CALL_EXPR (basetype)
  2601.        && name != TYPE_IDENTIFIER (basetype)
  2602.        && (TREE_CODE (function) != FUNCTION_DECL
  2603.            || strncmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function)),
  2604.                OPERATOR_METHOD_FORMAT,
  2605.                OPERATOR_METHOD_LENGTH))
  2606.          && (may_be_remote (basetype) || instance != C_C_D))
  2607.     {
  2608.       tree fn_as_int;
  2609.  
  2610.       parms = TREE_CHAIN (parms);
  2611.  
  2612.       if (!all_virtual && TREE_CODE (function) == FUNCTION_DECL)
  2613.     fn_as_int = build_unary_op (ADDR_EXPR, function, 0);
  2614.       else
  2615.     fn_as_int = convert (TREE_TYPE (default_conversion (function)), DECL_VINDEX (function));
  2616.       if (all_virtual == 1)
  2617.     fn_as_int = convert (integer_type_node, fn_as_int);
  2618.  
  2619.       result = build_opfncall (METHOD_CALL_EXPR, LOOKUP_NORMAL, instance, fn_as_int, parms);
  2620.  
  2621.       if (result == NULL_TREE)
  2622.     {
  2623.       compiler_error ("could not overload `operator->()(...)'");
  2624.       return error_mark_node;
  2625.     }
  2626.       else if (result == error_mark_node)
  2627.     return error_mark_node;
  2628.  
  2629. #if 0
  2630.       /* Do this if we want the result of operator->() to inherit
  2631.      the type of the function it is subbing for.  */
  2632.       TREE_TYPE (result) = value_type;
  2633. #endif
  2634.  
  2635.       return result;
  2636.     }
  2637. #endif
  2638.  
  2639.   if (parms == error_mark_node
  2640.       || (parms && TREE_CHAIN (parms) == error_mark_node))
  2641.     return error_mark_node;
  2642.  
  2643.   if (need_vtbl == needed)
  2644.     {
  2645.       function = build_vfn_ref (&TREE_VALUE (parms), instance,
  2646.                 DECL_VINDEX (function));
  2647.       TREE_TYPE (function) = build_pointer_type (fntype);
  2648.     }
  2649.  
  2650.   if (TREE_CODE (function) == FUNCTION_DECL)
  2651.     GNU_xref_call (current_function_decl,
  2652.            IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function)));
  2653.  
  2654.   {
  2655.     int is_constructor;
  2656.     
  2657.     if (TREE_CODE (function) == FUNCTION_DECL)
  2658.       {
  2659.     is_constructor = DECL_CONSTRUCTOR_P (function);
  2660.     TREE_USED (function) = 1;
  2661.     function = default_conversion (function);
  2662.       }
  2663.     else
  2664.       {
  2665.     is_constructor = 0;
  2666.     function = default_conversion (function);
  2667.       }
  2668.  
  2669.     result = build_nt (CALL_EXPR, function, parms, NULL_TREE);
  2670.  
  2671.     TREE_TYPE (result) = value_type;
  2672.     TREE_SIDE_EFFECTS (result) = 1;
  2673.     TREE_HAS_CONSTRUCTOR (result) = is_constructor;
  2674.     result = convert_from_reference (result);
  2675.     return result;
  2676.   }
  2677. }
  2678.  
  2679. /* Similar to `build_method_call', but for overloaded non-member functions.
  2680.    The name of this function comes through NAME.  The name depends
  2681.    on PARMS.
  2682.  
  2683.    Note that this function must handle simple `C' promotions,
  2684.    as well as variable numbers of arguments (...), and
  2685.    default arguments to boot.
  2686.  
  2687.    If the overloading is successful, we return a tree node which
  2688.    contains the call to the function.
  2689.  
  2690.    If overloading produces candidates which are probable, but not definite,
  2691.    we hold these candidates.  If FINAL_CP is non-zero, then we are free
  2692.    to assume that final_cp points to enough storage for all candidates that
  2693.    this function might generate.  The `harshness' array is preallocated for
  2694.    the first candidate, but not for subsequent ones.
  2695.  
  2696.    Note that the DECL_RTL of FUNCTION must be made to agree with this
  2697.    function's new name.  */
  2698.  
  2699. tree
  2700. build_overload_call_real (fnname, parms, flags, final_cp, buildxxx)
  2701.      tree fnname, parms;
  2702.      int flags;
  2703.      struct candidate *final_cp;
  2704.      int buildxxx;
  2705. {
  2706.   /* must check for overloading here */
  2707.   tree overload_name, functions, function, parm;
  2708.   tree parmtypes = NULL_TREE, last = NULL_TREE;
  2709.   register tree outer;
  2710.   int length;
  2711.   int parmlength = list_length (parms);
  2712.  
  2713.   struct candidate *candidates, *cp;
  2714.  
  2715.   if (final_cp)
  2716.     {
  2717.       final_cp[0].h.code = 0;
  2718.       final_cp[0].h.distance = 0;
  2719.       final_cp[0].function = 0;
  2720.       /* end marker.  */
  2721.       final_cp[1].h.code = EVIL_CODE;
  2722.     }
  2723.  
  2724.   for (parm = parms; parm; parm = TREE_CHAIN (parm))
  2725.     {
  2726.       register tree t = TREE_TYPE (TREE_VALUE (parm));
  2727.  
  2728.       if (t == error_mark_node)
  2729.     {
  2730.       if (final_cp)
  2731.         final_cp->h.code = EVIL_CODE;
  2732.       return error_mark_node;
  2733.     }
  2734.       if (TREE_CODE (t) == OFFSET_TYPE)
  2735. #if 0
  2736.       /* This breaks reference-to-array parameters.  */
  2737.       || TREE_CODE (t) == ARRAY_TYPE
  2738. #endif
  2739.     {
  2740.       /* Perform the conversion from ARRAY_TYPE to POINTER_TYPE in place.
  2741.          Also convert OFFSET_TYPE entities to their normal selves.
  2742.          This eliminates needless calls to `compute_conversion_costs'.  */
  2743.       TREE_VALUE (parm) = default_conversion (TREE_VALUE (parm));
  2744.       t = TREE_TYPE (TREE_VALUE (parm));
  2745.     }
  2746.       last = build_tree_list (NULL_TREE, t);
  2747.       parmtypes = chainon (parmtypes, last);
  2748.     }
  2749.   if (last)
  2750.     TREE_CHAIN (last) = void_list_node;
  2751.   else
  2752.     parmtypes = void_list_node;
  2753.  
  2754.   if (is_overloaded_fn (fnname))
  2755.     {
  2756.       functions = fnname;
  2757.       if (TREE_CODE (fnname) == TREE_LIST)
  2758.     fnname = TREE_PURPOSE (functions);
  2759.       else if (TREE_CODE (fnname) == FUNCTION_DECL)
  2760.     fnname = DECL_NAME (functions);
  2761.     }
  2762.   else 
  2763.     functions = lookup_name_nonclass (fnname);
  2764.  
  2765.   if (functions == NULL_TREE)
  2766.     {
  2767.       if (flags & LOOKUP_SPECULATIVELY)
  2768.     return NULL_TREE;
  2769.       if (flags & LOOKUP_COMPLAIN)
  2770.     error ("only member functions apply");
  2771.       if (final_cp)
  2772.     final_cp->h.code = EVIL_CODE;
  2773.       return error_mark_node;
  2774.     }
  2775.  
  2776.   if (TREE_CODE (functions) == FUNCTION_DECL && ! IDENTIFIER_OPNAME_P (fnname))
  2777.     {
  2778.       functions = DECL_MAIN_VARIANT (functions);
  2779.       if (final_cp)
  2780.     {
  2781.       /* We are just curious whether this is a viable alternative or
  2782.              not.  */
  2783.       compute_conversion_costs (functions, parms, final_cp, parmlength);
  2784.       return functions;
  2785.     }
  2786.       else
  2787.     return build_function_call_real (functions, parms, 1, flags);
  2788.     }
  2789.  
  2790.   if (TREE_CODE (functions) == TREE_LIST
  2791.       && TREE_VALUE (functions) == NULL_TREE)
  2792.     {
  2793.       if (flags & LOOKUP_SPECULATIVELY)
  2794.     return NULL_TREE;
  2795.       
  2796.       if (flags & LOOKUP_COMPLAIN)
  2797.     cp_error ("function `%D' declared overloaded, but no instances of that function declared",
  2798.           TREE_PURPOSE (functions));
  2799.       if (final_cp)
  2800.     final_cp->h.code = EVIL_CODE;
  2801.       return error_mark_node;
  2802.     }
  2803.  
  2804.   length = count_functions (functions);
  2805.   
  2806.   if (final_cp)
  2807.     candidates = final_cp;
  2808.   else
  2809.     {
  2810.       candidates
  2811.     = (struct candidate *)alloca ((length+1) * sizeof (struct candidate));
  2812.       bzero ((char *) candidates, (length + 1) * sizeof (struct candidate));
  2813.     }
  2814.  
  2815.   cp = candidates;
  2816.  
  2817.   my_friendly_assert (is_overloaded_fn (functions), 169);
  2818.  
  2819.   functions = get_first_fn (functions);
  2820.  
  2821.   /* OUTER is the list of FUNCTION_DECLS, in a TREE_LIST.  */
  2822.   for (outer = functions; outer; outer = DECL_CHAIN (outer))
  2823.     {
  2824.       int template_cost = 0;
  2825.       function = outer;
  2826.       if (TREE_CODE (function) != FUNCTION_DECL
  2827.       && ! (TREE_CODE (function) == TEMPLATE_DECL
  2828.         && ! DECL_TEMPLATE_IS_CLASS (function)
  2829.         && TREE_CODE (DECL_TEMPLATE_RESULT (function)) == FUNCTION_DECL))
  2830.     {
  2831.       enum tree_code code = TREE_CODE (function);
  2832.       if (code == TEMPLATE_DECL)
  2833.         code = TREE_CODE (DECL_TEMPLATE_RESULT (function));
  2834.       if (code == CONST_DECL)
  2835.         cp_error_at
  2836.           ("enumeral value `%D' conflicts with function of same name",
  2837.            function);
  2838.       else if (code == VAR_DECL)
  2839.         {
  2840.           if (TREE_STATIC (function))
  2841.         cp_error_at
  2842.           ("variable `%D' conflicts with function of same name",
  2843.            function);
  2844.           else
  2845.         cp_error_at
  2846.           ("constant field `%D' conflicts with function of same name",
  2847.            function);
  2848.         }
  2849.       else if (code == TYPE_DECL)
  2850.         continue;
  2851.       else
  2852.         my_friendly_abort (2);
  2853.       error ("at this point in file");
  2854.       continue;
  2855.     }
  2856.       if (TREE_CODE (function) == TEMPLATE_DECL)
  2857.     {
  2858.       int ntparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (function));
  2859.       tree *targs = (tree *) alloca (sizeof (tree) * ntparms);
  2860.       int i;
  2861.  
  2862.       i = type_unification (DECL_TEMPLATE_PARMS (function), targs,
  2863.                 TYPE_ARG_TYPES (TREE_TYPE (function)),
  2864.                 parms, &template_cost, 0);
  2865.       if (i == 0)
  2866.         function = instantiate_template (function, targs);
  2867.     }
  2868.  
  2869.       if (TREE_CODE (function) == TEMPLATE_DECL)
  2870.     {
  2871.       /* Unconverted template -- failed match.  */
  2872.       cp->function = function;
  2873.       cp->u.bad_arg = -4;
  2874.       cp->h.code = EVIL_CODE;
  2875.     }
  2876.       else
  2877.     {
  2878.       struct candidate *cp2;
  2879.  
  2880.       /* Check that this decl is not the same as a function that's in
  2881.          the list due to some template instantiation.  */
  2882.       cp2 = candidates;
  2883.       while (cp2 != cp)
  2884.         if (cp2->function == function)
  2885.           break;
  2886.         else
  2887.           cp2 += 1;
  2888.       if (cp2->function == function)
  2889.         continue;
  2890.  
  2891.       function = DECL_MAIN_VARIANT (function);
  2892.  
  2893.       /* Can't use alloca here, since result might be
  2894.          passed to calling function.  */
  2895.       cp->h_len = parmlength;
  2896.       cp->harshness = (struct harshness_code *)
  2897.         oballoc ((parmlength + 1) * sizeof (struct harshness_code));
  2898.  
  2899.       compute_conversion_costs (function, parms, cp, parmlength);
  2900.  
  2901.       /* Make sure this is clear as well.  */
  2902.       cp->h.int_penalty += template_cost;
  2903.  
  2904.       if ((cp[0].h.code & EVIL_CODE) == 0)
  2905.         {
  2906.           cp[1].h.code = EVIL_CODE;
  2907.           cp++;
  2908.         }
  2909.     }
  2910.     }
  2911.  
  2912.   if (cp - candidates)
  2913.     {
  2914.       tree rval = error_mark_node;
  2915.  
  2916.       /* Leave marker.  */
  2917.       cp[0].h.code = EVIL_CODE;
  2918.       if (cp - candidates > 1)
  2919.     {
  2920.       struct candidate *best_cp
  2921.         = ideal_candidate (NULL_TREE, candidates,
  2922.                    cp - candidates, parms, parmlength);
  2923.       if (best_cp == (struct candidate *)0)
  2924.         {
  2925.           if (flags & LOOKUP_COMPLAIN)
  2926.         {
  2927.           cp_error ("call of overloaded `%D' is ambiguous", fnname);
  2928.           print_n_candidates (candidates, cp - candidates);
  2929.         }
  2930.           return error_mark_node;
  2931.         }
  2932.       else
  2933.         rval = best_cp->function;
  2934.     }
  2935.       else
  2936.     {
  2937.       cp -= 1;
  2938.       if (cp->h.code & EVIL_CODE)
  2939.         {
  2940.           if (flags & LOOKUP_COMPLAIN)
  2941.         error ("type conversion ambiguous");
  2942.         }
  2943.       else
  2944.         rval = cp->function;
  2945.     }
  2946.  
  2947.       if (final_cp)
  2948.     return rval;
  2949.  
  2950.       return buildxxx ? build_function_call_real (rval, parms, 0, flags)
  2951.         : build_function_call_real (rval, parms, 1, flags);
  2952.     }
  2953.  
  2954.   if (flags & LOOKUP_SPECULATIVELY)
  2955.     return NULL_TREE;
  2956.   
  2957.   if (flags & LOOKUP_COMPLAIN)
  2958.     report_type_mismatch (cp, parms, "function",
  2959.               decl_as_string (cp->function, 1));
  2960.  
  2961.   return error_mark_node;
  2962. }
  2963.  
  2964. tree
  2965. build_overload_call (fnname, parms, flags, final_cp)
  2966.      tree fnname, parms;
  2967.      int flags;
  2968.      struct candidate *final_cp;
  2969. {
  2970.   return build_overload_call_real (fnname, parms, flags, final_cp, 0);
  2971. }
  2972.  
  2973. tree
  2974. build_overload_call_maybe (fnname, parms, flags, final_cp)
  2975.      tree fnname, parms;
  2976.      int flags;
  2977.      struct candidate *final_cp;
  2978. {
  2979.   return build_overload_call_real (fnname, parms, flags, final_cp, 1);
  2980. }
  2981.